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"
forfiles.exe /p D:\Files /s /m *.* /d -7 /c "cmd /c del @file"
/p | This parameter specifies the path that contain the files I wish to delete. |
/s | This parameter tells the program to recurse into any subfolders to look for additional files. |
/m | If 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 (*.*). |
/d | This 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. |
/c | This 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. |
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.
ReplyDeletePowershell -command "& {powershell_command}
Example:
Powershell -command "& {get-wmiobject -class win32_share -computer localhost}