Solved

Deleting old files except four.

Posted on 2009-04-01
7
261 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now