Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delete files older than a specified date from a vbscript

Posted on 2014-04-09
12
Medium Priority
?
1,823 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 28

Expert Comment

by:MacroShadow
ID: 39988702
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1000 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 59

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 28

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 28

Assisted Solution

by:MacroShadow
MacroShadow earned 500 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 500 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 59

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

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!

Question has a verified solution.

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

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

886 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