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

Posted on 2005-04-25
Last Modified: 2008-02-01

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.

Question by:gran3085

    Author Comment

    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:


    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_"

    LVL 10

    Accepted Solution

    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
    'wscript.echo "Earliest file is " & FH.Name & " (" & CStr (FH.DateLastModified) & ")"
    Set f = Nothing
    Set FH = Nothing
    Set Fol = nothing
    Set fso = nothing

    Regards .. Alan

    Author Comment


    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
    What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 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

    18 Experts available now in Live!

    Get 1:1 Help Now