Link to home
Start Free TrialLog in
Avatar of pucko73
pucko73

asked on

Delete files older than a specified date from a vbscript

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*")
Avatar of aikimark
aikimark
Flag of United States of America image

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?
Avatar of pucko73
pucko73

ASKER

I mean files that was created or modified Before a date I specify
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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Inside the FOR EACH OBJFILE loop, you can just add a check of the file name to match your needs.

~bp
Actually I added the If objFile.name = "*.log" Then ;)
Avatar of pucko73

ASKER

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....
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of pucko73

ASKER

Thanks!