0

I'm trying to delete all directories based off a naming convention using a batch file in windows. I don't want to delete all directories, only one's that match a pattern.

I'm doing this on Windows Server, so I don't know if that matters. I can get this to work on my personal desktop, but when I attempt on the Windows Server computer it doesn't work.

Echo Deleting Folders

cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp

pause

For /D /r %%i in ("*mail*") DO rd /Q /S %%i

pause

echo Done

When this is run, it iterates through all the directories and lists them. But then after it lists them all it says the following:

"The system cannot find the file specified."

"The system cannot find the path specified."

I find this odd since it literally lists every path and then says it can't find it. I'm sure I'm missing something small. Any help is appreciated.

Solved:

Echo Deleting Folders

cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp

pause


for /F "delims=" %%I in ('dir /S /B /A:D "*mail*" ^| sort /R') do @rd /S /Q "%%I"

pause

echo Done
Compo
  • 36,585
  • 5
  • 27
  • 39
Chuck D
  • 11
  • 3
  • 1
    You shouldn't be using two recursive functions, as it is likely that `RD /S` removes some of the tree that `For /R` has identified, but not yet acted upon. You can probably just send the STDERR output to NUL, but I would suggest you alter your methodology instead. – Compo Aug 20 '19 at 13:12
  • Yes, and because [`for [/R]` does not enumerate the whole directory tree in advance](https://stackoverflow.com/q/31975093). So you could do: `for /F "delims=" %%I in ('dir /S /B /A:D "C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp\*mail*" ^| sort /R') do @rd /S /Q "%%I"` (`dir /S /B` parsed by `for /F` ensures enumeration of the directory tree in advance, and `sort /R` ensures to process the deepest directory hierarchy levels first). By the way, use `cd /D` instead of pure `cd`... – aschipfl Aug 20 '19 at 13:20
  • It works on my desktop 100% of the time. I'm not sure what the difference is. – Chuck D Aug 20 '19 at 13:21
  • Thanks aschipfl. Your method worked. – Chuck D Aug 20 '19 at 13:26

1 Answers1

0
Echo Deleting Folders

cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp

pause


for /F "delims=" %%I in ('dir /S /B /A:D "*mail*" ^| sort /R') do @rd /S /Q "%%I"

pause

echo Done
Compo
  • 36,585
  • 5
  • 27
  • 39
Chuck D
  • 11
  • 3