?
Solved

How do I delete the oldest file in a folder using VBScript

Posted on 2005-04-25
3
Medium Priority
?
680 Views
Last Modified: 2008-02-01
Hi,

How do I search a specified folder "C:\security" and delete the oldest (last modified) file in that folder?

I want to run this on a Windows 2000 computer so I need an answer using VBScript / WMI.

Thanks
0
Comment
Question by:gran3085
  • 2
3 Comments
 

Author Comment

by:gran3085
ID: 13857196
Sorry I missed a bit.

What I actually want to do is scan a specified folder and delete the oldest file begining with "secuirty_"

In other words I have a folder called "C:\Security" with 5 files in it named:

secuirty_1.log
secuirty_2.log
secuirty_3.log
secuirty_4.log
someotherfile.log

Lets say that the "someotherfile.log" is actually the oldest and security_4.log is the next oldest. In this case I'd want to delete the secuirty_4.log file as it is the oldest file that starts with "security_"

Thanks
Pete
0
 
LVL 10

Accepted Solution

by:
ADSaunders earned 2000 total points
ID: 13857223
Hi gran3085,
Try this,
Copy & paste to a .vbs file. Query your spelling above secuirty_ I have spelt as Security .. You may need to change it.

Dim fso, Fol, f, LowDt, FH
Set fso = CreateObject("Scripting.FileSystemObject")
Set Fol = fso.GetFolder("C:\Security")
Lowdt = DateAdd("d",1,Date()) ' Start from a date later than it could possibly be (tomorrow)
For Each f In Fol.Files
    If f.DateLastModified < Lowdt _
    And Left (f.Name, Len("Security_")) = "Security_" Then ' Check Spelling!!!!
        Lowdt = f.DateLastModified
        Set FH = f ' Store Handle
    End If
Next
'wscript.echo "Earliest file is " & FH.Name & " (" & CStr (FH.DateLastModified) & ")"
FH.Delete
Set f = Nothing
Set FH = Nothing
Set Fol = nothing
Set fso = nothing

Regards .. Alan
0
 

Author Comment

by:gran3085
ID: 13857306
Alan,

Thanks, thats perfect. I had to add "On error resume next" as it errored when there was no file to delete. But thanks fine.
Nice work!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

839 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