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
Solved

Delete files older than a specified date from a vbscript

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

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39988702
0
 
LVL 53

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 53

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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