Solved

Deleting old files except four.

Posted on 2009-04-01
7
267 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
[X]
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
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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