Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Deleting old files except four.

Posted on 2009-04-01
7
Medium Priority
?
271 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

916 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