Solved

Delete files older than a specified date from a vbscript

Posted on 2014-04-09
12
1,672 Views
Last Modified: 2014-04-09
Hello.

I like to find all files Starting with  MyIni
in the path that I can find in "%USERPROFILE%"  but only
if they are older than a specified date. (Say first of january 2014 for example)
(IE se the code below... But I need to do that only for files older than a specified date.)
Can someone help me?

Set objShell =  CreateObject("Wscript.Shell")
strUsrProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile (strUsrProfile&"\WINDOWS\MyIni*")
0
Comment
Question by:pucko73
[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
  • 3
  • 3
  • 3
  • +1
12 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 39988600
When you write "older than a specified date", do you mean the files' modified dates are less or equal to a specified date or greater than a specified date?
0
 

Author Comment

by:pucko73
ID: 39988614
I mean files that was created or modified Before a date I specify
0
 
LVL 46

Expert Comment

by:aikimark
ID: 39988672
If you're already using FSO, then you will need to iterate all the files in the folder and inspect both the name and the modified date before you delete it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39988702
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 39988721
Here is my script from that alternate thread that might suit your needs, it allows flexibility in what files are deleted, if subfolders are searched, and if empty subfolders are removed.  See if it might meet your needs.

' Set processing mode flags
Const UseCreatedDate = "C"
Const UseModifiedDate = "M"
Const UseAccessedDate = "A"

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Remove old files and as needed
' Parms are: Base folder, days to keep files, file date to use for age check, process subfolders flag, remove empty folders flag
RemoveOldFiles objFSO.GetFolder("d:\"), 30, UseCreatedDate, True, False
RemoveOldFiles objFSO.GetFolder("e:\temp"), 90, UseModifiedDate, True, False
RemoveOldFiles objFSO.GetFolder("f:\"), 30, UseCreatedDate, True, False

Sub RemoveOldFiles(objFolder, intDays, strDateToUse, blnProcessSubFolders, blnRemoveEmptyFolders)
    On Error Resume Next

    ' Remove any files older than specified days from this folder
    For Each objFile In objFolder.Files
        
        ' Get the desired date for the age check        
        Select Case UCase(strDateToUse)
           Case "C"
              datFile = objFile.DateCreated
           Case "M"
              datFile = objFile.DateLastModified
           Case "A"
              datFile = objFile.DateLastAccessed
           Case Else
              datFile = objFile.DateLastModified
        End Select

        If DateDiff("d", datFile, Now) > intDays Then
            If objFile.name = "*.log" Then
                Wscript.Echo "Deleted file: " & objFile.Path
                objFile.Delete
            End If
        End If
    Next

    ' See if we were able to access this folder, if not don't recurse into it
    If Err.Number = 0 Then
        If blnProcessSubFolders Then
            ' Remove all older files in any subfolders of this one
            For Each objSubFolder In objFolder.Subfolders
                RemoveOldFiles objSubFolder, intDays
            Next
        End If

        If blnRemoveEmptyFolders Then
            ' If folder is now empty, remove it
            If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
                Wscript.Echo "Deleted folder: " & objFolder.Path
                objFolder.Delete
            End If
        End If
    End If
End Sub

Open in new window

~bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39988727
Inside the FOR EACH OBJFILE loop, you can just add a check of the file name to match your needs.

~bp
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39988893
Actually I added the If objFile.name = "*.log" Then ;)
0
 

Author Comment

by:pucko73
ID: 39988905
Ok. all the sugested things abowe are doing almost the same as I found searching the net Before I posted here.  They are using a variable to find files older than nDays.

I want to find files older than for example 2014-01-01  
(ie a specified date)

And that is what I don't know how to get in there....
0
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 125 total points
ID: 39988933
Populate the variable:
nDays = DateDiff("d", #1/1/2014#, Date())

Open in new window

0
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 125 total points
ID: 39988974
You can skip the DateDiff() calculation and just compare the date to the cut-off date.
Example:
If objFile.DateLastModified < #1/1/2014# Then
   objFile.Delete
End If

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39989007
Either of the last two post will get you there, you can just calculate the number of days from your cutoff date to today and then run the script using that number of days.  Or you can change the script slightly to take in a cutoff date rather than a number of days.  If you want help working in that change let me know, happy to help.

~bp
0
 

Author Closing Comment

by:pucko73
ID: 39989034
Thanks!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

622 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