Page 1 of 1

Unable to produce truthful info about deletion or lack of thereof [SOLVED]

Posted: 13 Jul 2023 04:39
by DOSadnie
I have this code

Code: Select all

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

del /q "%THIS-WILL-BE-DELETED%\*.*"

dir "%THIS-WILL-BE-DELETED%" 1>NUL 2>&1

if errorlevel 1 (
    echo Files were deleted at
    echo.
    echo C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker

) else (
    echo There was nothing to delete at
    echo.
    echo C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker
)

pause
which is suppose to inform me if files were or were not deleted - but it always uses the text from >>else<< i.e. it lies about not deleting

How can this be fixed?

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 13 Jul 2023 19:18
by ShadowThief
Delete the folder instead of emptying it, then use if exist to see if the folder is there. dir will always return something when it's inside of a folder that exists, so it's always going to return an %errorlevel% of zero.

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 15 Jul 2023 04:50
by DOSadnie
I cannot delete it as it has sub-folders that I need to keep

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 24 Jul 2023 03:29
by Batcher
dir /b /a-d "%THIS-WILL-BE-DELETED%" 1>NUL 2>&1

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 24 Jul 2023 09:11
by DOSadnie
This did not help: I still get Files were deleted at reported even if nothing has been deleted

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 24 Jul 2023 11:42
by ShadowThief
Why do you even want the "There was nothing to delete" message? Whether there was or was not anything in the folders before the script was run, there is currently nothing in the folders after the fact. The message is useless.

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 24 Jul 2023 14:41
by DOSadnie
Such message is not useless if it is a part of a large script that deals with erasing of various clutter from many locations- thus reading some parts of an aftermath raport is sometimes vital to me

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 24 Jul 2023 20:25
by Batcher

Code: Select all

set "THIS-WILL-BE-DELETED=C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker"
dir /b /a-d "%THIS-WILL-BE-DELETED%" 1>NUL 2>&1
if not errorlevel 1 (
    del /q "%THIS-WILL-BE-DELETED%\*.*"
    echo Files were deleted at
) else (
    echo There was nothing to delete at
)

Re: Unable to produce truthful info about deletion or lack of thereof

Posted: 25 Jul 2023 14:17
by DOSadnie
This works, thank you

I have adjusted it slightly to this:

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