Hi Deepak,
The code that you just provided, is for a Unix environment. I required help for a Windows Batch script.
Main Topics
Browse All TopicsI need help writing a Batch Script to delete files that are older than 5 days.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Use forfiles.exe.
http://ss64.com/nt/forfile
This is version 1.1 so is most compatible. A newer version for Vista and 2003 but is not compatible with Windows 2000. Slight variation in syntax.
This will list your files that are older than 5 days.
forfiles -pC:\Folder -s -d-5
Remove the echo from the code to have it delete the file.
Or better still, this one as it handles filenames containing spaces:
@echo off
echo wscript.echo DateAdd("d",-5,date)>"5days
for /f "tokens=2-4 delims=/ " %%a in ('cscript //nologo "5daysago.vbs"') do (
for /f "tokens=*" %%A in ('dir /a-d /b *.*') do (
for /f "tokens=2-4 delims=/ " %%d in ('echo %%~tA') do (
if %%f%%d%%e leq %%c%%a%%b del "%%A"
)
)
)
:: Create a one-line VB script file containing just the following commend: "DateAdd("d",-5,date)"
:: This uses the VB function 'DateAdd' to add or subtract a certain number of days to the current date.
:: When run, it will return a string corresponding to the date (plus or minus the number of specified days).
echo wscript.echo DateAdd("d",-5,date)>"5days
:: This FOR loop is a container loop whose purpose is to run the VB script file and capture it's output.
:: The output format is 'ddd mm/dd/yyyy' - we need just the 'dd' 'mm' and 'yyyy' (2nd, 3rd and 4th parts).
:: The output is parsed using spaces and the '/' character as delimiters (or field seperators).
:: %%a will equal the 'mm' part, %%b will equal the 'dd' part and %%c will equal the 'yyyy' part.
:: So for "Fri 23/10/2009", '%%c%%a%%b' will equal "20091023"
for /f "tokens=2-4 delims=/ " %%a in ('cscript //nologo "5daysago.vbs"') do (
:: This FOR loop is a container loop whose purpose is to execue the DIR command and capture it's output.
:: The 'DIR /A-D /B *.*' lists only filenames of files (using DOS's DIR bare format) and alloes for spaces.
:: The filename of each file is captured by %%A
for /f "tokens=*" %%A in ('dir /a-d /b *.*') do (
:: This FOR loop is a container loop whose purpose is to execute an ECHO command capturing it's output
:: In this case we're ECHOing %%~tA which is the timestamp of the file in %%A
:: The output format is "ddd mm/dd/yyyy" - we need just the 'dd' 'mm' and 'yyyy' (2nd, 3rd & 4th parts again).
:: The output is parsed using spaces and the '/' character as delimiters (or field seperators).
:: %%d will equal the 'mm' part, %%e will equal the 'dd' part and %%f will equal the 'yyyy' part.
:: So for "Fri 28/10/2009", '%%f%%d%%e' will equal "20091028"
for /f "tokens=2-4 delims=/ " %%d in ('echo %%~tA') do (
:: This IF statement compares the file's date against the target date returned by the VB function 'DateAdd()'.
:: %%A is the filename.
:: %%c%%a%%b is 20091023 - the date given by the VB function
:: %%f%%d%%e is 20091028 - the file's date
:: So, the following lijne reads "if 20091028 is less than or equal to 20091023 then delete the file in %%A"
if %%f%%d%%e leq %%c%%a%%b del "%%A"
To answer your question, to specify a target directory, you need to add it to the 2nd FOR loop as in the following example for "C:Temp\":
for /f "tokens=*" %%A in ('dir /a-d /b "C:\Temp\*.*"') do (
and it's a batch file - not a VB script file however, the batch file creates a single-line VB script file which it executes solely becuase it's the simplest method for calculating relative dates.
Business Accounts
Answer for Membership
by: kosarajudeepakPosted on 2009-08-17 at 18:08:07ID: 25119539
You can use the following:
Select allOpen in new window