DOS batch file or script that can delete files in a subfolder by date

Have weekly image backups of each of our machines to a network drive with their respected machine names.  

Network Server
   Machine1 (folder)

Next month it would start Week5.img, the newest image.

Would like to have Week1.img automatically deleted so there are only four images per machine folder instead of accumulating every week and taking up network server space.

Once I have the logic for one machine I can then add the other machines that need to have their folders maintained.  Then can schedule that in Windows Scheduler.

Who is Participating?
Try this.  It will parse through all folders in d:\images and delete the oldest file in each main subfolder
@echo off
Set CurDir = "D:\Images"
cd %CurDir%
for /f %%f in ('dir /ad /b') do (Call :DelOld %%f)
Set list=%1
cd %CurDir%\%list%
for /f %%f in ('dir *.img /a-d /o-d /b') do (set myfile=%%f)
del %myfile%

Open in new window

Try this attached batch file.  Just make sure this is run in the correct directory or set the directory in the code.

It will delete the oldest file with a .img extension
@echo off
for /f %%f in ('dir *.img /a-d /o-d /b') do (set myfile=%%f)
del %myfile%

Open in new window

ltlbearand3's suggestion will work well, but I suggest using  /od  instead of  /o-d  in the for-loop so that the oldest file will get deleted instead of the newest.  (points to ltbearand3)
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Thanks for double checking my work.  However, I think you want /o-d since it will loop through all files and delete the last one in the list.  Therefore, you want the list newest first.

To test the batch file you can run the attached code and it will just display the file that would be deleted instead of deleting it.

@echo off
for /f %%f in ('dir *.img /a-d /o-d /b') do (set myfile=%%f)
Echo %myfile%

Open in new window

JLTollefsonAuthor Commented:
Simple and effective!

One more question

Have the folder structure of...

       server1 (subfolder of images)
       server2 (subfolder of images)
       server3 (subfolder of images)
       server4 (subfolder of images)

In each of the server directories are the .img files

With the batch file can I simply state to go to each of these directories and perform the

@echo off
for /f %%f in ('dir *.img /a-d /o-d /b') do (set myfile=%%f)
del %myfile%
JLTollefsonAuthor Commented:
Thanks again for the very quick, easy to understand, and simple solution for this issue.
Shanmuga SundaramDirector of Software EngineeringCommented:
If you never mind in using vbs then see the code below. I have used to just display the name and have commented the delete line. So please first ensure that it works fine and then if you are sure then remove the commented line to delete the file. Be precautious.
Option Explicit
On Error Resume Next
    Dim oFSO
    Dim sDirectoryPath
    Dim oFolder
    Dim oFileCollection
    Dim ofoldercollection
    Dim oFile
    Dim iDaysOld
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    set ofolder = ofso.getfolder("C:\your path")
    Set ofoldercollection = oFolder.subfolders
    For Each oFile In oFileCollection
        if ucase(right(oFile.Name,3)) = "IMG" then msgbox 
'oFile.Delete (True)    
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFileCollection = Nothing
    Set oFile = Nothing

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.