Search code examples
batch-fileforfiles

Ignore one folder and its contents in batch operation


I've tried to create a batch file that will remove all files and sub-directories from a folder. The command I found that worked is this

FORFILES /p “X:\DAILY\1 MONDAY” /m *.* /c “cmd /c Del /F /Q @path” /d -7 /s & FORFILES /p “X:\DAILY\1 MONDAY” /S /D -7 /C “cmd /c IF @isdir == TRUE rd /S /Q @path”

Now my users tell me there is a Reference folder under 1 MONDAY that they don't want purged (but all other subdirectories should be emptied and deleted.) Can anyone advise how I might accomplish that?

Thank you!


Solution

  • It is extremely inefficient to use FORFILES for such a task because FORFILES has to start cmd.exe for each file found to delete the file older than seven days and for each folder to remove.

    A much better solution would be:

    @echo off
    %SystemRoot%\System32\robocopy.exe "X:\DAILY\1 MONDAY" "X:\DAILY\1 MONDAY\WeeklyDelete" /E /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" /MINAGE:7 /MOVE /NDL /NFL /NJH /NJS
    if exist "X:\DAILY\1 MONDAY\WeeklyDelete\" rd /Q /S "X:\DAILY\1 MONDAY\WeeklyDelete"
    

    ROBOCOPY searches

    • for all files in the directory X:\DAILY\1 MONDAY
    • and its subdirectories including empty directories because of option /E with
    • excluding the files in the two directories X:\DAILY\1 MONDAY\WeeklyDelete and X:\DAILY\1 MONDAY\Reference and all their subdirectories because of option /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" with
    • excluding all files last modified in last seven days because of option /MINAGE:7.

    The found files and folders matching theses criteria are moved to X:\DAILY\1 MONDAY\WeeklyDelete whereby the destination folder is automatically created on not already existing.

    The options /NDL /NFL /NJH /NJS are for not printing the list of moved directories, list of moved files, the header and the summary.

    A folder is moved only if being empty after moving all files matched by these criteria. So a folder is not moved on containing a file or subfolder.

    The option /S instead of /E and the option /MOV instead of /MOVE can be used to move only files and do not move also folders being empty before or after moving the files.

    The file and folder movements are done very fast by ROBOCOPY because of destination folder is on same drive as source folder which means just the file system of this drive must be updated and no file data must be moved at all.

    An IF condition is used after ROBOCOPY finished with updating the file system to move the files and folders to verify if the destination folder X:\DAILY\1 MONDAY\WeeklyDelete exists. In this case command RD is used to delete this folder quietly because of option /Q and with all files and subdirectories because of option /S. This action is again just a file system update not really deleting file data stored on storage media and so is processed very fast.

    Open a command prompt window and execute there the following commands to read more about the used commands in the three lines above.

    • echo /?
    • if /?
    • rd /?
    • robocopy /?

    The commands are described also