Scheduling log files deletion on server with forfiles

Sharing is caring!

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">The following batch file uses &&num;8220&semi;<i>Forfiles&period;exe<&sol;i>&&num;8221&semi; for deleting files from the mentioned path&period; <i>Forfiles&period;exe<&sol;i> is supported only by Windows Vista&comma; Windows 7&comma; Windows Server 2003&comma; and higher versions&period; Forfiles is present in the following path&colon; <i>c&colon;&gt&semi;windows&gt&semi;system32<&sol;i>&period; Before running the batch file&comma; make sure that <i>forfiles&period;exe<&sol;i> is present in the path otherwise the batch file will not work<&sol;p>&NewLine;<p>There are many other features for Forfiles but I have mentioned only few here&period;<&sol;p>&NewLine;<p>Following command will delete IIS logs from exchange box &comma; will run every Sunday&comma; and will delete files in that folder older than 30 days&period;&nbsp&semi;&nbsp&semi; <&sol;p>&NewLine;<p>Windows 2003<br &sol;>at 12&colon;00 &sol;EVERY&colon;Su Forfiles&period;exe -p C&colon;WINDOWSsystem32LogFilesW3SVC1 -m &ast;&period;log -d -30 -c &&num;8220&semi;Cmd&period;exe &sol;C del &commat;path&&num;8221&semi; <&sol;p>&NewLine;<p>Windows 2008<br &sol;>at 12&colon;00 &sol;EVERY&colon;Su Forfiles&period;exe -p C&colon;inetpublogsLogFilesW3SVC1 -m &ast;&period;log -d -30 -c &&num;8220&semi;Cmd&period;exe &sol;C del &commat;path&&num;8221&semi;<&sol;p>&NewLine;<p>To list all of the batch files on drive C&comma; type&colon;<&sol;p>&NewLine;<pre>forfiles &sol;p c&colon; &sol;s &sol;m &ast;&period;bat &sol;c "cmd &sol;c echo &commat;file is a batch file"<&sol;pre>&NewLine;<p>&nbsp&semi; <&sol;p>&NewLine;<pre>Syntax<br &sol;> FORFILES &lbrack;&sol;p <i>Path<&sol;i>&rsqb; &lbrack;&sol;<b>m<&sol;b><i> SrchMask<&sol;i>&rsqb; &lbrack;&sol;s&rsqb; &lbrack;&sol;<b>c<&sol;b><i> Command<&sol;i>&rsqb; &lbrack;&sol;<b>d<&sol;b><i> <&sol;i>&lbrack;&plus; &vert; -&rsqb; &lbrace;<i>date<&sol;i> &vert; <i>dd<&sol;i>&rcub;&rsqb; <br &sol;>Key<br &sol;> &sol;p <i>Path<&sol;i> The Path to search &lpar;default&equals;current folder&rpar;<br &sol;> &sol;m <i>SrchMask<&sol;i> Select files matching the specified search mask<br &sol;> default &equals; &ast;&period;&ast;<br &sol;> &sol;s Recurse into sub-folders<br &sol;> &sol;C <i>command<&sol;i> The command to execute for each file&period;<br &sol;> Wrap the command string in double quotes&period;<br &sol;> Default &equals; "cmd &sol;c echo &commat;file"<br &sol;> The Command variables listed below can also be used in the<br &sol;> command string&period;<br &sol;> &sol;D <i>date<&sol;i> Select files with a last modified date greater than or <br &sol;> equal to &lpar;&plus;&rpar;&comma; or less than or equal to &lpar;-&rpar;&comma;<br &sol;> the specified <i>date<&sol;i>&comma; using the region specific date format<br &sol;> typically "MM&sol;DD&sol;yyyy" or "DD&sol;MM&sol;yyyy"<br &sol;> &sol;D &plus; <i>dd<&sol;i> Select files with a last modified date greater than or<br &sol;> equal to the current date plus "dd" days&period; &lpar;in the future&rpar;<br &sol;> &sol;D - <i>dd<&sol;i> Select files with a last modified date less than or<br &sol;> equal to the current date minus "dd" days&period; &lpar;in the past&rpar;<br &sol;><br &sol;> A valid "dd" number of days can be any number in<br &sol;> the range of 0 to 32768&period; &lpar;89 years&rpar;<br &sol;> "&plus;" is taken as default sign if not specified&period;<&sol;pre>&NewLine;<&sol;div>&NewLine;

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.