Tuesday, October 30, 2012

Delete Files older then X Days BATCH SCRIPT

To delete files older then X Days use the following script:

forfiles.exe /p D:\Files /s /m *.* /d -7 /c "cmd /c del @file"
/pThis parameter specifies the path that contain the files I wish to delete.
/sThis parameter tells the program to recurse into any subfolders to look for additional files.
/mIf you want to specify a specific file type, this parameter will allow you to limit the search to specific files, such as *.doc for Word documents. In my case, I looked for all files (*.*).
/dThis one is the key parameter – it specifies the last modified date value. In my example I specify “-7″ which indicates that the files need to have a modified date 7 days less than the current date.
/cThis is the command that I execute on the files found by the program. The delete command is executed in a command window for each file.
 

1 comment:

  1. I've used [forfiles.exe] for quite a few things. You may also find the following string handy when you're using a batch script and need to execute a powershell command.

    Powershell -command "& {powershell_command}

    Example:
    Powershell -command "& {get-wmiobject -class win32_share -computer localhost}

    ReplyDelete