batch file - FORFILES in FOR loops issues -


i trying make script remove empty folders , delete files number of days old. depending on txt file delimiters set to. have came far:

::batch set cdid=%~dp0 set test=true  if %test%==true (     set comnd1=echo     set comnd2=echo     ) else (     set comnd1=del     set comnd2=rd     ) echo file ran %date:~10%/%date:~4,2%/%date:~7,2% >>%cdid%\log.txt  /f "usebackq delims=| tokens=1,2" %%x in (%cdid%path.txt) (     call :del_folders "%%x" %%y     call :del_files "%%x" %%y     )  goto :eof  :del_files     forfiles /p %1 /s /m *.* /d %2 /c "cmd /c %comnd1% @file"     goto :eof  :del_folders     /f "delims=" %%i in ('dir %%1 /s /b /ad ^| sort /r') %comnd2% "%%i"      goto :eof   ::path.txt c:\temp\blank|10 c:\temp\new folder|30 

when run script @file not populate , %%i not populate, not sure doing wrong. help?

you made couple of small errors. in del_folders used %%1 meant argument not expanded (you needed 1 % here). did not handle case there no files match or directories empty. in forfiles command put /m *.*; although documentation says default, documentation incorrect. missing out /m matches files (the default) saying /m *.* match files dot!

my corrected version is:

::batch set cdid=%~dp0 set test=true  if %test%==true (     set comnd1=echo     set comnd2=echo     ) else (     set comnd1=del     set comnd2=rd     ) echo file ran %date:~10%/%date:~4,2%/%date:~7,2% >>%cdid%\log.txt  /f "usebackq delims=| tokens=1,2" %%x in (%cdid%path.txt) (     call :del_folders "%%x" %%y     call :del_files "%%x" %%y     )  goto :eof  :del_files     forfiles /p %1 /s /d %2 /c "cmd /c %comnd1% @file" 2> nul     goto :eof  :del_folders     /f "delims=" %%i in ('dir "%~1" /s /b /ad 2^>nul ^| sort /r') %comnd2% "%%i"     goto :eof   ::path.txt c:\temp\blank|10 c:\temp\new folder|30 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -