But what you are doing is checking the "Date created" date of the file correct ? I need to check the date inside the file name like mentioned in the example.
Main Topics
Browse All TopicsI have a folder which contains several text files with different names. These files are generated everyday with the server's current date. I have these files for example: "CLIENT_20091029.txt", "CLIENT_20091030.txt", "CLIENT_20091031.txt", "CLIENT_20091101.txt", "CLIENT_20091102.txt", "CLIENT_20091103.txt", "CLIENT_20091104.txt". I need to create a batch file (.bat) to delete all files with the date of 5 days old from current date. I need to keep only the file which are not more then 5 days old. So using the files I mentioned as example the batch file will delete the files with the dates 20091029 and 20091030 and keep the others. How do I do this with a batch file ?
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.
BTW is there only one file per day? If so then the easiest way without involving dates at all is to delete all but the last five (in filename order or date as appropriate). You can do this with a for loop reading a dir listing of the files, e.g.
@echo off
for /f "skip=5 tokens=*" %%a in ('dir /b /o-n "CLIENT_*.txt"') do echo Need to delete %%a
Should do it -- it take a dir listing of the spec. shown in reverse name order (i.e. newest first), skipping the first 5 entries. If this gives you the files you want you can replace the echo %%a with a del "%%a" command. We can add in and sort out paths to the files as needed too of course.
Steve
Hi,
The script does work, I use this on a Vista machine (with no resource kit / support tools add-on installed) to delete log files created on a windows 2003 server. Perhaps this does not work on your Operating system.
However I take your point that it uses the "Date Created" rather than your date in the filename although if your are creating a file using "the server's current date" isn't that the same thing. Applogies & Good Luck !!
YZF-R1 gave a valid solution under normal circumstances but it appears that it did not match the requirements of Fredy992 due to it working based on the date, while he required by filename.
My simpler solution of picking the last 5 files by name depends upon naming convention etc. but seems it would work with the example filenames at least with a date coded in them in reverse order as they have. - I know I use it or similar for aging a whole host of log files already.
So without the OP's input I would say both are valid solutions for a problem of this nature but to Accept mine as it is more likely to satisfy the requirements here?
Accept http:25739173 150 points
Assisted: 25738605 100 points
Does that sound fair... split it 50:50 if preferred, is only 250.
Steve
I agree totally with dragon-it. Unfortemetely the OP did not IMHO explain fully or offer any further information.
I would also recommend dragon-it proposal.
Accept http:25739173 150 points
Assisted: 25738605 100 points
Business Accounts
Answer for Membership
by: YZF-R1Posted on 2009-11-04 at 04:27:28ID: 25738605
Hi Fredy992,
This will delete all txt files older than 5 days (use with caution) D:\test2 will need renaming to your chosen directory.
forfiles /p d:\test2 /s /m *.txt /d -5 /c "cmd /c del @file : date >= 5 days"
Hope this helps ?