One can read for hours and hours online about the best way to purge files and folders (and sub directories). Everyone seems to have their own iteration.
This is what I came up with. Essentially, a path (less the trailing "\" is passed to a function and then I run a FORFILES over it to remove all files from the current and subdir's that match the specified age. Then I use the ROBOCOPY techniques to copy itself with the /S /MOVE switches to remove the empty folders.
Is this an OK approach?
Code: Select all
CALL :PURGE ""C:\Hyperion_Batch\Errors","C:\Hyperion_Batch\Logs","C:\Hyperion_Batch\Archives""
:PURGE
SET "PURGE=%~1"
FOR %%D IN ( %PURGE:,= % ) DO (
SET "PDIR=%%~D"
ECHO "!PDIR!" | FINDSTR /C:"Archive" >nul 2>&1 && ( SET "AGE=60" & GOTO BREAK )
ECHO "!PDIR!" | FINDSTR /C:"Errors" >nul 2>&1 && ( SET "AGE=30" & GOTO BREAK )
ECHO "!PDIR!" | FINDSTR /C:"Logs" >nul 2>&1 && ( SET "AGE=30" & GOTO BREAK )
SET "AGE=30"
:BREAK
FORFILES /P "!PDIR!" /S /M *.* /D -!AGE! /C "CMD /C DEL @PATH"
ROBOCOPY "!PDIR!" "!PDIR!" /S /MOVE
)
pause
GOTO :EOF