Solved

Delete files older than a specified date from a vbscript

Posted on 2014-04-09
12
1,404 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
  • 3
  • 3
  • 3
  • +1
12 Comments
 
LVL 45

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 45

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

Expert Comment

by:MacroShadow
ID: 39988702
0
 
LVL 52

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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 45

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now