Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2007-12-06
7
4,823 Views
Last Modified: 2010-05-18
Have weekly image backups of each of our machines to a network drive with their respected machine names.  

Network Server
   Machine1 (folder)
      Week1.img
      Week2.img
      Week3.img
      Week4.img

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.

Thanks
0
Comment
Question by:JLTollefson
7 Comments
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 20425226
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

0
 
LVL 33

Expert Comment

by:knightEknight
ID: 20425349
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)
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 20427022
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%
Pause

Open in new window

0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:JLTollefson
ID: 20429970
Simple and effective!

One more question

Have the folder structure of...

D:\images
       server1 (subfolder of images)
       server2 (subfolder of images)
       server3 (subfolder of images)
       server4 (subfolder of images)
       etc.

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%
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 500 total points
ID: 20430452
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)
Exit
 
:DelOld
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

0
 

Author Closing Comment

by:JLTollefson
ID: 31413306
Thanks again for the very quick, easy to understand, and simple solution for this issue.
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20484265
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.name 
'oFile.Delete (True)    
    Next
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFileCollection = Nothing
    Set oFile = Nothing

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBScript on Html 15 47
excel VBA - clear duplicates not working 11 36
incorrect syntax near the order by 10 41
VBA Vbscript  Issue 9 25
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question