Info about deletion lies when no items were deleted when dealing with sub-folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Info about deletion lies when no items were deleted when dealing with sub-folders

#1 Post by DOSadnie » 29 Jul 2023 03:30

I have this script

Code: Select all

@echo off

set "ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED=C:\q\Corel Draw - CDR Auto-Backup Files\"

setlocal enabledelayedexpansion

    echo.    Deleted files at
    echo.
    echo  %ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%
    echo.
    echo.

for /r "%ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%" %%F in (*) do (
    echo.
    del "%%F" /q
    echo                                          %%F
)

    echo.
    echo.
    echo.

    echo. Deleted sub-folders at
    echo.
    echo     "%ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%"
    echo.
    echo.

for /f "delims=" %%D in ('dir /ad /b /s "%ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%" 2^>nul ^| sort') do (
    echo.
    rd "%%D" /s /q 2>nul
    echo                                            "%%D"
)

endlocal

pause

cmd /k
Its purpose is to

1] go to C:\q\Corel Draw - CDR Auto-Backup Files\

2]
delete all files

3] present a report listing alphabetically all deleted files showing their paths

4] delete all folders

5] preset a report listing alphabetically all deleted folders showing their path

and / or: report a lack of occurrence of deletion process

Unfortunately that last caveat is not working as this script always says
Deleted files at
and / or
Deleted sub-folders at

I already got the same problem solved here viewtopic.php?f=3&t=10852 resulting with such script

Code: Select all

@echo off

set "FILES-IN-THIS-FOLDER-WILL-BE-DELETED=C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker"

dir /b /a-d "%FILES-IN-THIS-FOLDER-WILL-BE-DELETED%" 1>nul 2>&1

if not errorlevel 1 (
    echo. Deleted files at
    echo.
    echo  %FILES-IN-THIS-FOLDER-WILL-BE-DELETED%
    echo.
    echo.

    for %%F in ("%FILES-IN-THIS-FOLDER-WILL-BE-DELETED%\*.*") do (
        echo.
        echo                                                                      %%~nxF
        del /q "%%F"
    )
) else (
    echo. There was nothing to be deleted at
    echo.
    echo  %FILES-IN-THIS-FOLDER-WILL-BE-DELETED%
)

pause
but this one is for different conditions as its purpose is to delete only files but leave out sub-folders and their content intact
Last edited by DOSadnie on 06 Apr 2024 02:21, edited 8 times in total.

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Info about deletion lies when no items were deleted when dealing with sub-folders

#2 Post by Squashman » 31 Jul 2023 22:25

Set a variable inside the loop. The variable will only be defined if the execution of the FOR command has data to parse. Then echo if the variable is defined.

Code: Select all

@echo off

set "ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED=C:\q\Corel Draw - CDR Auto-Backup Files\"

set "file_flag="
for /r "%ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%" %%F in (*) do (
    set "file_flag=Y"
    echo.
    del "%%F" /q
    echo                                          %%F
)
IF DEFINED file_flag (
    echo.    Deleted files at
    echo.
    echo  %ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%
    echo.
    echo.
)

set "folder_flag="
for /f "delims=" %%D in ('dir /ad /b /s "%ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%" 2^>nul ^| sort') do (
    set "folder_flag=Y"
    echo.
    rd "%%D" /s /q 2>nul
    echo                                            "%%D"
)

IF DEFINED folder_flag (
    echo. Deleted sub-folders at
    echo.
    echo     "%ITEMS-IN-THIS-FOLDER-WILL-BE-DELETED%"
    echo.
    echo.
)

Post Reply