Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting old files except four.

Posted on 2009-04-01
7
Medium Priority
?
269 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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

722 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