Simple Script To Delete Files Automatically Older Than X Days Using Batch Files
Simple Script To Delete Files Automatically Older Than X Days Using Batch Files
Provide any task name that we can identify in Name field, Click Next
Select the period in Task Trigger Page that when you want to delete we have to
decide that how much the files are bigger and what capacity of Disk we have , here I
selected Daily, Click Next
I selected the Start Date and time that my 1st check has to start and I select Recur
every as 1 a day
Select the batch file which we had written, select the folder where we had kept the
batch file, Click Next
Select Open the Properties dialog for this task when I click Finish
Click Finish
Here we have to select Run Whether user is logged on or not, and Click OK
Thats done.
We schedule a task in the windows Task scheduler that Daily at 10:00:00 AM It will run the
batch script file and delete the files which are older than 15 days automatically.
Explanation:
Using forfiles command I want to check for the files in f:\logs folders for 10 days back files,
I want to delete those files if they are before 10 days.
Parameters for fofiles:
Parameter
Description
/p <Path>
Specifies the path from which to start the search. By default, searching starts in the
working directory.
/m <SearchMask>
Searches files according to the specified search mask. The default search mask is *
/s
/c "<Command>"
Runs the specified command on each file. Command strings should be enclosed in
The default command is "cmd /c echo @file".
/d [{+|-}][{<Date>|
Selects files with a last modified date within the specified time frame.
Selects files with a last modified date later than or equal to (+) or earlier than or equ
<Days>}]
specified date, where Date is in the format MM/DD/YYYY.
Selects files with a last modified date later than or equal to (+) the current date plus
days specified, or earlier than or equal to (-) the current date minus the number of
Valid values for Days include any number in the range 032,768. If no sign is specifie
default.
/?
You can use the following variables in the command string as specified by
the /c command-line option.
Variable
Description
@FILE
File name.
@FNAME
@EXT
@PATH
@RELPATH
@ISDIR
@FSIZE
@FDATE
@FTIME
Reference : http://technet.microsoft.com/en-us/library/cc753551.aspx