Question Locked

MS-DOS code to delete old Files

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am using windows XP and need to write a DOS batch (.bat) file that will delete old files from a directory. This will run on a schedule and will not require human interaction to input any data. My problem is, I am not good at writing batch files. I can do the basic 101 type stuff, but finding dates and doing date comparison is beyond me.

This batch file will need to check the current date and then compare this date with the file date. If a file is found to be greater than, say, 3 months old then delete that file, then go to the next file and repeat the process until the end of file is reached.

Is anyone out there good with writing DOS batch files and can give me some help on this? If you can also recommend a good free online site where I can go to understand the syntax of finding the file date and how to compare dates? I have found several tutorials but they do not touch on dates. Thanks.

8 replies

by jck

write a batch that uses xcopy to copy files by date into a temp folder, then delete the temp folder.

by kenone

forfiles /p d:\Foldername /s /m *.* /d -10 /c "cmd /c del @file : date >= 90 days"

You could run it as a scheduled task.
Try it out on a tmp folder with unnecessary files first.

by jutpro

Hi Kenone, thank you for your response.

I ran the batch file at the DOS prompt and the "forfiles" command is not recognized. I received the below error message;

'forfiles' is not recognized as an internal or external command,operable program or batch file.

by kenone

I run it from a server to clean out FTP files every night.

by kenone

download and copy forfiles.exe into the system32 folder and it works with XP SP3.
I did that so long ago that I forgot that I had done it.

by jutpro

Thank you Kenone. I appreciate all your help.

This discussion is closed to new replies.