Solved

Delete files older than a specified date from a vbscript

Posted on 2014-04-09
12
1,364 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 26

Expert Comment

by:MacroShadow
ID: 39988702
0
 
LVL 51

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 51

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 26

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 26

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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

19 Experts available now in Live!

Get 1:1 Help Now