MS-DOS code to delete old Files

480 pts.
Batch files
Microsoft Windows
Windows Batch Files
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.

Software/Hardware used:
Windows XP

Answer Wiki

Thanks. We'll let you know when a new response is added.

Here’s a VBScript for this activity.

I like using the ForFiles utility for this purpose.

I found this batch file also. Be sure to test first in a test environment.

IF "%DAY%" == "Monday" GOTO FIRST

forfiles /p d:\FTPData /s /m *.* /d -2 /c "cmd /c del @file : date >= 1 day > NUL"

forfiles /p d:\FTPData /s /m *.* /d -4 /c "cmd /c del @file : date >= 3 days > NUL"



Thank you for your response.

I ran the batch file at the DOS prompt and the “forfiles” command is not recognize . I received the below error message;

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

Discuss This Question: 3  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Troy Tate
    Click on the forfiles link in the answer above. You will need to download the utility.
    0 pointsBadges:
  • jutpro
    Ok, I had overlooked the link and did not realize I had to do a download. Thank you Troy and Labnuke99.
    480 pointsBadges:
  • Durgaprasad72
    Hi, I am trying to delete folders which are 3 days old. Folder size is more than 400MB. There are around 6-7 folders in this location. But the script i tried is deleting 1-2 folders. I have used the below code in my VBS file. strFolderPath = "C:TestBuilds" 'Folder Path strLogFolderPath="C:TestBuildsLogs" NumberOfDays = 3 'Anything older than this many days will be removed On Error Resume Next Dim fso,objFolder,objFile,objSubfolder,objLogFolder Set fso = CreateObject("Scripting.FileSystemObject") Set objFolder = fso.GetFolder(strFolderPath) Set objLogFolder = fso.GetFolder(strLogFolderPath) 'DELETE all subfolders in Folder Path older than Number of days For Each objSubfolder In objFolder.Subfolders If objSubFolder.Name = "Logs" Then ' Set objSubFolder= Nothing Else If DateDiff("d", objSubfolder.DateCreated,Now) > NumberOfDays Then objSubfolder.Delete True Wait 20 End If End If Next 'Delete all the folders in Log folder For Each objSubfolder In objLogFolder.Subfolders If DateDiff("d", objSubfolder.DateCreated,Now) > NumberOfDays*5 Then objSubfolder.Delete True End If Next Set fso=Nothing Set objFolder=Nothing Set objLogFolder=Nothing Set objSubfolder=Nothing Can anyone suggest me on this. - Durga Prasad
    10 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: