Scripts to automatically delete specified files on specified folders with a schedule ***500 points

Posted on 2006-04-21
Last Modified: 2010-04-18
Hello Experts,

I have logs file that I would like to delete daily using windows scheduler. Can some one write me a complete solution using window batch or script ? and show me how to setup a schedule to run every night STEP BY STEP?  Thank you very much.

I would like to be able to specify folder and file wild card like below, and I can remove any entry and add new entries.

Question by:andreni78
    LVL 48

    Assisted Solution

    Hi andreni78,

    here is some code that you place directly into a notepad file and save as whatever.vbs. This will delete anything with a .gz extensions

    You can of course modify this to do whatever folders you like
    Dim fso
    ' Delete all .gz files within folder c:\Logs\Folder1 and c:\Logs\Folder2 and c:\Logs\Folder3

       Set fso = CreateObject("Scripting.FileSystemObject")

       Set fso = CreateObject("Scripting.FileSystemObject")

       Set fso = CreateObject("Scripting.FileSystemObject")

    to add this as a schedulaed task you need to go to start - programs - Accesories - system tools - Scheduled Tasks

    run through the wizard and add your whatever.vbs file as the task to run, add the time you want it to run and you may want to run it is as the administrator (part of the wizard...)

    LVL 7

    Accepted Solution

    In addition to Jay Jay70 solution you can also do this with .bat file

    @echo off
    del C:\Logs\Folder1\*.gz
    del C:\Logs\Folder2\*.gz
    del C:\Logs\Folder3\*.gz

    copy the above 4 lines in notepad and save it as <file name>.bat

    whether it is .VBS or .BAT file when you are saving it through notepad make sure "Save as Type" changed to All Fles otherwise it will be saved as

    filename.VBS.txt or filename.BAT.txt
    LVL 48

    Expert Comment

    only if you have file extensions hidden :)
    LVL 48

    Expert Comment

    did the second answer given work? i tried it but it would never delete all the files and alwyas wanted confirmation

    what was i missing
    LVL 7

    Expert Comment

    Hi JayJay70, It works but since you mentioned it is asking for conformation i just tried again, it is deleteing all the files with out asking for any confirmation.

    May i know how you did it ?
    LVL 48

    Expert Comment

    ahh what i was doing wrong was putting in *.* and trialling - it wouldnt delete certain files but not briefcase for example

    i tried that option first the wronte a vbscript - first one ever lol!

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    How can I check multiple Servers up time? 6 67
    DHCP IP Address problem 5 50
    Instant VM Recovery 4 62
    Trust one-way issue 2 45
    Numerous times I have been asked this questions that what is it that makes my machine log on so slow, there have been cases where computers took 23 minute exactly after taking password and getting to the desktop. Interesting thing was the fact th…
    A quick step-by-step overview of installing and configuring Carbonite Server Backup.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now