Simple script to delete files automatically older than x days using batch files.



Script  to delete files older than x days using batch files  and how to schedule them using windows task manager

Script:

@echo off

forfiles /p "f:\logs" /d -10 /c "cmd /c del @file " 



Save this file as a batch file with extension .bat



How to Schedule in Task Scheduler



  • Click Start, Accessories, System Tools , Task Scheduler
  • Click on Create Basic Task
  • 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
  • I have selected start a Program in Action Page


  • 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 
That’s 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 current working directory.
/m <SearchMask>
Searches files according to the specified search mask. The default search mask is *.*.
/s
Instructs the forfiles command to search into subdirectories recursively.
/c "<Command>"
Runs the specified command on each file. Command strings should be enclosed in quotation marks. The default command is "cmd /c echo @file".
/d [{+|-}][{<Date>|<Days>}]
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 equal to (-) the 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 the number of days specified, or earlier than or equal to (-) the current date minus the number of days specified. 
·    Valid values for Days include any number in the range 0–32,768. If no sign is specified, + is used by default.
/?
Displays help at the command prompt.

  • Forfiles is most commonly used in batch files.
  • Forfiles /s is similar to dir /s.
  • You can use the following variables in the command string as specified by the /c command-line option. 

Variable
Description
@FILE
File name.
@FNAME
File name without extension.
@EXT
File name extension.
@PATH
Full path of the file.
@RELPATH
Relative path of the file.
@ISDIR
Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE.
@FSIZE
File size, in bytes.
@FDATE
Last modified date stamp on the file.
@FTIME
Last modified time stamp on the file.

Comments

Popular posts from this blog

Observium: Configuring Microsoft Windows 2008 Server SNMP Agent

AWS: Upgrade PV Drivers on Windows Instances

How to configure Incremental backups for MSSQL Database