Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
DOSadnie
- Posts: 143
- Joined: 21 Jul 2022 15:12
- Location: Coding Kindergarten
#1
Post
by DOSadnie » 13 Jul 2023 04:39
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?
Last edited by
DOSadnie on 25 Jul 2023 14:17, edited 1 time in total.
-
ShadowThief
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#2
Post
by ShadowThief » 13 Jul 2023 19:18
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.
-
DOSadnie
- Posts: 143
- Joined: 21 Jul 2022 15:12
- Location: Coding Kindergarten
#3
Post
by DOSadnie » 15 Jul 2023 04:50
I cannot delete it as it has sub-folders that I need to keep
-
Batcher
- Posts: 74
- Joined: 16 Apr 2009 10:36
#4
Post
by Batcher » 24 Jul 2023 03:29
dir /b /a-d "%THIS-WILL-BE-DELETED%" 1>NUL 2>&1
-
DOSadnie
- Posts: 143
- Joined: 21 Jul 2022 15:12
- Location: Coding Kindergarten
#5
Post
by DOSadnie » 24 Jul 2023 09:11
This did not help: I still get Files were deleted at reported even if nothing has been deleted
-
ShadowThief
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#6
Post
by ShadowThief » 24 Jul 2023 11:42
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.
-
DOSadnie
- Posts: 143
- Joined: 21 Jul 2022 15:12
- Location: Coding Kindergarten
#7
Post
by DOSadnie » 24 Jul 2023 14:41
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
-
Batcher
- Posts: 74
- Joined: 16 Apr 2009 10:36
#8
Post
by Batcher » 24 Jul 2023 20:25
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
)
-
DOSadnie
- Posts: 143
- Joined: 21 Jul 2022 15:12
- Location: Coding Kindergarten
#9
Post
by DOSadnie » 25 Jul 2023 14:17
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