I needed to write short scripts to delete old files on a Windows Server. It’s a very easy task on UNIX flavors but the limited capacity of Windows command line made me worried. You can use a simple “find” command to find old files on UNIX. For example, the following command will be enough to find and delete files older than 7 days:
1 |
find /folder/ -type f -mtime +7 -exec rm {} + |
For the ones who are not familiar with the find command, I’ll try to explain its parameters. The parameter “-type f” is not mandatory but it will help you find only “files” (and filter out directories etc). The parameter “exec” lets you run a command for the selected files. The brackets are replaced with the name of the selected file. The plus sign (+) will make the command line is built by appending each selected file name at the end.
While searching the net, I found that Windows has a useful command to handle mass files. Here’s the Windows version of the above command:
1 |
forfiles /p "folder" /s /D -7 /C "cmd /c del @path" |
You need to enter negative values to find old files on windows. The parameter “/s” is to search into subdirectories recursively, and the parameter “/c” is to run a command. @path is the full path name of the selected file. It’s better than I expected 🙂