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

Deleting old files except four.

Posted on 2009-04-01
7
264 Views
Last Modified: 2012-05-06
Hello,

I wan to erase files older than 30 days except four in my windows 2003 server. I find the following script in other post, but i don't now how to modify it for do file exceptions.

Const ForAppending = 8
strSourceFolder = "C:\Testing"
strLog = "C:\FilesRemoved.Log"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOutputFile : Set objOutputFile = objFSO.CreateTextFile(Replace(strLog, ".Log", Month(Date) & Day(Date) & Year(Date) & "_.Log"))
 
With objOutputFile
  .WriteLine "==========================="
  .WriteLine "Removal summary for " & Date
  .WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=-="
  .WriteLine
End With
 
'intDel is the number of days old you want to check for.  I set it to 1 for testing.
intDel = 30
dtOld = DateAdd("d", -intDel, Date)
 
Set objFolders = objFSO.GetFolder(strSourceFolder)
'Get the files in the current folder
 
For Each fil In objFolders.Files
  'If a file exists that hasn't been accessed in intDel days or more, delete it
  If DateDiff("d", fil.DateCreated, dtOld) >= intDel Then
    objOutputFile.WriteLine fil.Path & " was removed at " & Now
    objFSO.DeleteFile fil.Path
  End If
Next
 
With objOutputFile
  .WriteLine
  .WriteLine "Process completed at " & Now
  .WriteLine "==========================="
  .Close
End With
 
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit

Thanks for your help.
0
Comment
Question by:DGWOLF
  • 5
  • 2
7 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24040626
What are the exceptions that make you want to keep those four files? Or, are you saying you want to remove
all but four files that are older than 30 days?
0
 

Author Comment

by:DGWOLF
ID: 24049588
It's a backup folder. I do a diferential backup everyday, and i want to remove all old backup but not the firts backup done, because it's necessary to restore.

Thanks.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24052059
OK, let me work on it. Not used to windows scripting. we'll have to sort the filelist by date first and then do the deletions that way.
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:DGWOLF
ID: 24248348
Anybody can't help me?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24250083
Sorry, forgot about this one. Let me work on it some today.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 24252900
Try this, it's not tested but should lead you to an answer:
CONST adFldIsNullable = &H00000020 
CONST adVarChar = 200 
CONST adDate = 7 
CONST adOpenDynamic = 2 
CONST adUseClient = 3 
 
Set fso = CreateObject("scripting.filesystemobject") 
Set folder = fso.getFolder("C:\EddieADS\Office_SQL\files") 
Set intDel = 30
Set dtOld = DateAdd("d", -intDel, Date)
 
if folder.files.count > 0 then 
    Set rs = CreateObject("ADODB.Recordset") 
 
    With rs 
        .CursorLocation = adUseClient 
        .CursorType = adOpenDynamic 
        .Fields.Append "filename", adVarChar, 255, adFldIsNullable 
        .Fields.Append "filemod", adDate, 32, adFldIsNullable 
        .open 
 
        for each file in folder.Files 
            .AddNew 
            .Fields("filename") = file.Name 
            .Fields("filemod") = file.dateLastModified 
            .update 
        next 
 
        ' list oldest first 
 
        .Sort = "filemod ASC" 
        .MoveFirst
        do while not .eof
          if DateDiff("d", rs.fields("filemod"), dtOld) >= intDel Then
            .Delete
          .MoveNext
        loop
        .Movefirst
 
        for each file in folder.Files 
          if rs.Find("filename = " & file.Name) then
              fso.DeleteFile(file.Path)
          end if 
        next 
        .close 
    end with 
    set rs = nothing 
end if 
set folder = nothing 
set fso = nothing 

Open in new window

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24252918
oh, I forgot to check for the four, you do it in the do while loop and delete the first four from the recordset, then when you search for them in the for each at the bottom, it will not delete those four you deleted from the recordset.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 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