Forum Moderators: open

Message Too Old, No Replies

Delete by date batch file

         

Sybok

2:23 pm on Apr 7, 2006 (gmt 0)

10+ Year Member



Hi

I am trying to write a batch file in XP, that deletes all the files in a folder that have a certian time stamp e.g. 5 days old.

Is there any way to do this. i must confess i am pretty rusty at my DOS prompt kung-fu. ;)

bill

2:17 am on Apr 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There are a few ways to do this sort of batch file. The easiest I've seen uses the Forfiles [technet2.microsoft.com] which allows you to select files with a date greater than or equal to (+) or less than or equal to (-) the specified date. This is a bit easier than setting up date variables and calculations in my book. Forfiles was available in some of the resource kits. I've used the one from Win2K on XP without any noticeable problems.

Sybok

8:50 am on Apr 10, 2006 (gmt 0)

10+ Year Member



Thanks bill for your help, can you tell me if this line of code is right? (i dont have win2k3 server and I wont have a chance to test it until i am at the place it is supposed to go). Ultimate goal is for the batch file to scan the dir and delete all the files older than 5 days.

forfiles /p c:\backup /s /m *.* /d t-5 /c "cmd /c del @file : date >= 5 days"

Will this work?

bill

9:02 am on Apr 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That does follow the same syntax as used on the MS site so I don't see why not. Test it out with some sample data on your live system though to make sure it does.

Sybok

9:30 am on Apr 10, 2006 (gmt 0)

10+ Year Member



OK managed to get it on a win2k3 server, and this error pops up, which i wasn't too suprised because i probably made a mistake.... until...

C:\>2.bat

C:\>forfiles /p c:\backup /s /m *.* /d t-5 /c "cmd /c del @file : date >= 5 days
"
ERROR: Invalid date specified.
Type "FORFILES /?" for usage.

... I input the microsoft example from the link you sent me:

C:\>1.bat

C:\>forfiles /p c:\ /s /m *.* /d t-100 /c "cmd /c echo @file : date >= 100 days"

ERROR: Invalid date specified.
Type "FORFILES /?" for usage.

What is going on? not even the microsoft example works on it? please please help i am going out of my mind.

bill

9:57 pm on Apr 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I noticed in some of the examples they eliminated the space after the
/d
switch. Try that:
/dt-100

instead of:
/d t-100

Also make sure you've got some files in the target folder that meet the criteria.

Another idea would be to run the

/?
switch and make sure all the switches you're running in the batch file are listed.