0

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!
Compo
  • 36,585
  • 5
  • 27
  • 39
GooGleye
  • 41
  • 1
  • 1
  • 4
  • 3
    The `FOR` variable is local to the `FOR` command itself. Once you are outside of the `FOR` command execution, you can no longer use that variable. If you need more than one command to execute with a `FOR` command then use an opening and closing parentheses around them. `for /f "delims=" %%G in ('dir /b /s *.0**.rfa') do ( ...all your commands....)` The commands can be on multiple lines. The key is to use the parentheses. – Squashman Nov 24 '20 at 14:30
  • 2
    The wildcard `*` means "any number of chars". `**` makes no sense ("any number of chars followed by any number of chars" == "any number of chars") – Stephan Nov 24 '20 at 15:08
  • @Squashman could you not establish a variable for the entire script instead of adding everything to the loop? – GooGleye Nov 24 '20 at 15:08
  • @Stephan yes I realised that as well... thanks – GooGleye Nov 24 '20 at 15:09
  • "could you not establish a variable for the entire script" - let me answer that with a *very* careful [yes](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script) – Stephan Nov 24 '20 at 15:13

1 Answers1

0

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
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I'd set the filemask into a variable (say, `filemask`) and then use that variable to avoid parallel-maintenance. – Magoo Nov 24 '20 at 18:54
  • I'm really a novice but this would basically be an optimized version of my original script where I ran the del command just like in your example? The library is pretty large and can take a long time to search through which is why I wanted to pass the del command only to files found in dir, to avoid searching the entire library twice. – GooGleye Nov 25 '20 at 15:08
  • hm - both `dir` and `del` have to traverse through the whole tree (and so will take similar times). How many *matching* files do you expect? If there are "just a few" (compared to the sum of all files), array-like variables (see the link in my last comment to your question) could be a solution. Or (probably at the same speed): `dir ... >files.txt`, `type files.txt`, `choice...`, and `for /f "delims=" %%a in (files.txt) do del ...` . Without showing the files beforehand, I expect a pure `del /s /q ...` to be the fastest possible solution. – Stephan Nov 25 '20 at 15:50
  • When I get to del I get "The system cannot find the file type.". I also suspect I will have trouble with Swedish characters here, looking in files.txt special characters are not formatted correctly. However if I do type files.txt the output is correct. Maybe it's just easier to do dir first and del after and accept the longer processing time. – GooGleye Nov 26 '20 at 12:58
  • did you remember to use `dir` with the `/b` and the `/a-d` switches? Check, if the file contains only full qualified filenames (no header, summary or date/time stamps and sizes). Trouble with Swedish characters shouldn't happen (as long as you write and read using the same codepage (`chcp /?`) – Stephan Dec 10 '20 at 18:12