Remove files time limit

From INIwiki
Jump to: navigation, search

How to remove files after a period of time

[edit] Linux

find /path/to/files* -mtime -5 -exec rm -rf {} \;

This will remove files older than 5 days

Substitue /path/to/files* with the directory you want to clean out, and 5 with the number of days old the tossed files should be. To test out what will get deleted, run the command without the -exec rm {} \;.

Then schedule with cron

Source: http://lifehacker.com/software/command-line/cli-fun--delete-files-older-than-x-days-239124.php

[edit] Windows

Also see Folder Sweeper

Use the program forfiles found here (rename file to have the exe file extension) Once you have a copy of forfiles.exe you write a simple batch file- pretty much one line:

forfiles /P PATHTOFOLDER /S /D -30 /M *.* /C "cmd /C del @path /Q"

Where PATHTOFOLDER is the full path to the directory in question (c:\windows\temp in your case?), the /S means "... and all subdirectories" if desirable, and the -30 after /D is "older than 30 days"; you can change to -10 if you want 10 days, etc. /M is file mask (*.* for all files) and /C is the command you'd run on any file matching these parameters, in this case "delete" with the /Q switch to avoid prompting.


Also, in some cases you may want it to get rid of folders as well, in which case you can add a second command to use /C "cmd /C rd @path /Q" at the end instead of the del @path /Q, which should quietly error out on non-folders but otherwise remove old folders after the files have been cleaned up. That might make your batch file look like this:

forfiles /P PATHTOFOLDER /D -30 /M *.* /C "cmd /C del @path /Q"
forfiles /P PATHTOFOLDER /S /D -30 /M *.* /C "cmd /C rd @path /Q"

Try this on a copy of a folder first to test it out...

[edit] Schedule in AT or task manager

Save this command line in a simple text CleanTemp.bat file using notepad- you can repeat the line multiple times in the CleanTemp.bat file if you want to do this with more than one folder as a target, or using the "remove directory" example I mentioned above. Every time you run it, it'll execute those commands in the background. Then have that scheduled to run via task scheduler and it's GUI, or the AT command at a command propmt, whichever is simpler for you. Ex:

at 9:00a /every:Su,M,T,W,Th,F,S c:\scripts\CleanTemp.bat

where c:\scripts\cleantemp.bat is the full path to the batch file. This will run it automatically at 9am every single day. You can type AT at the command line with no arguments to see any existing schedule jobs. Adjust to taste, add seasoning.


Source: http://ask.metafilter.com/35075/Automatic-file-maintenance-under-XP
Personal tools