1.

Solve : Delete subdirectories with spaces?

Answer»

Dear experts,

The appended script is extracted from Sidewinder:

set count=0
for /f %%i in ('dir /a:d /b /o:-n d:\test') do ( call :delete %%i)
goto :eof:delete
set /a count=%count%+1
if %count% GEQ 3 rd d:\test\%1 /s /q

and it works on subdirecectories like D:\Test\20060112

I also have subdirectories like D:\Test\2006 Feb 01
which have spaces in between. I have tried the FOLLOWING:

for /f %%i in ('dir /a:d /b /o:-n d:\test') do ( call :delete "%%i"
also
for /f delims= %%i in ('dir /a:d /b /o:-n d:\test') do (call :delete %%i

but it does not work for subdirectories like D:\Test\2006 Feb 01 where there
are spaces in between.

I need help.

Thanks in advance.
One of the problems with batch coding is that you end up coding the data, not the method, resulting in batch files that are very specific to a situation. The FOR statement uses spaces and tabs as the default delimiter. ADDING an override delims= clause should fix this.

This little snippet should help you out:

Code: [Select]@echo off
set count=0
for /f "delims=" %%i in ('dir /a:d /b /o:-n d:\test') do (
call :delete "%%i"
)
goto :eof

:delete
set /a count=%count%+1
if %count% GEQ 3 rd "d:\test\%1" /s /q

8-)

Note: There will be an extra quote generated by the code. If you don't mind, neither will the processor.

Dear Sidewinder,

Thank you very much.

You may want to hold off on the thanks. Once again I missed the obvious. When the dates were numeric (YYYYMMDD), the sort worked fine, but now that you have literal months this approach will get all fouled up. Consider: with all things being equal, February, April, August, and DECEMBER will all sort before January. Probable not what you want.

This should work more BETTER and get rid of the extra quote (actually it will add some extra spaces, but again the interpreter won't mind). A Windows Script would be a better solution, but most people see scripting as programming and run for the hills when it's mentioned.

Code: [Select]@echo off
set count=0
for /f "delims=" %%i in ('dir /a:d /b /o:-d d:\test') do (
call :delete %%i
)
goto :eof

:delete
set /a count=%count%+1
if %count% GEQ 0 rd "d:\test\%1 %2 %3" /s /q

As mentioned, you end up coding the data. You could also try adding the /s SWITCH to the dir command, but that may introduce a whole new set of problems.

Better luck this time. 8-)



Discussion

No Comment Found