Batch delete files in variable

634 Views Asked by At

I have a batch file which searches through a directory tree deleting generated file backups.

I want to edit the script to run the del command against the files found in the search, but I can't get it to work.

I've searched other threads and set it up similarly but without the expected results.

@echo off
pushd FILEPATH
echo Searching directories...
for /f "delims=" %%G in ('dir /b /s *.0**.rfa') do echo "%%G"
echo.
IF /I "%%G" == "" GOTO NOTFOUND 
set /P delete="Delete files? [Y/N]: "
IF /I "%delete%" NEQ "Y" GOTO ENDOF
echo Deleting files...
echo.
del "%%G"
echo.
echo Done!
timeout 5
exit

:ENDOF
echo Aborted.
timeout 5
exit

:NOTFOUND
echo Found nothing.
timeout 5
exit

Result:

Deleting files...

Could Not Find FILEPATH\ %G
 
Done!
1

There are 1 best solutions below

5
On

Do you really need the for loop? The following should work exactly as you want:

@echo off
dir /b /s "*.0*.rfa" || echo Found nothing. & goto :finish
choice /m "delete all? "
if errorlevel 2 echo Aborted. & goto :finish
echo Deleting files...
del /q /s "*.0*.rfa"
echo Done.
:finish
timeout 5
exit /b