Thursday, July 05, 2012

Execute a command for a batch in Windows 2008 Server


This is a command in windows 20008 for applying specified command on each file for a batch. 
For example if want to delete files older than a specified time period then this command will help me accomplishing this task. 
This is a very useful and easy to understand command. 
Command for an example is given below : 

The mentioned below command will delete files older than 15 days in the folder "D:\YOURFOLDERNAME" and any sub directories in it. 

forfiles /p d:\yourfoldername /s /m *.txt /c "cmd /c del @file" /d -15

forfiles --command name
/p -- specifies that next argument is path 
/s -- specifies that sub directories should be scanned
/m -- file type or search criteria 
/c -- actual command which will be executed for each file
"cmd /c" and "@file" are default values for the argument /c
"cmd /c  yourcommand @file"
/d --specifies number of days 

Further details can referenced on mentioned below technet link. 

Thanks to TechNet.