Schedule delete for files older than

Posted on 2009-02-20
Last Modified: 2012-05-06

I have a folder in windows 2008 server that has some log files generated every 5 minutes.

How can I make schedule auto delete for files older than 3 hours.


Question by:zaki100
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    you did not yet google around for that, did you :)

    anyhow, you need 2 parts:
    ? create a vbscript (easiest for me, at least), that deletes files older than X in a certain folder (+ subfolders, if needed)
    ? create a windows scheduled task to run that vbscript file (cscript yourscript.vbs)


    Author Comment

    Thank you for your help

    I have tried some of them but it deletes all files in a folder regardless file date time.

    I need a direct tested answer using bat file


    LVL 142

    Accepted Solution


    let me start with my .vbs file:
    Dim Fso
    Dim Directory
    Dim Folders
    Dim SubFolder
    Dim Modified
    Dim Files
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set Directory = Fso.GetFolder("D:\backupSQL")
    Set Folders = Directory.SubFolders
    for each SubFolder in Folders
      Set Files = SubFolder.Files
      For Each Modified in Files
        If DateDiff("D", Modified.DateLastModified, Now) > 2 Then 
        End If

    Open in new window

    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    the matching .bat file contains this:

    cscript d:\backupSQL\delete_old_files.vbs

    Open in new window


    Author Comment

    Thank you for your help

    I will try and let you know.



    Featured Post

    Are your corporate email signatures appalling?

    Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

    Join & Write a Comment

    With the higher take up rate of SAN’s, virtualisation etc, windows devices with more than one network interface are becoming more common.  As a general rule when a service that is installed on a Windows operating system is running, it only listens o…
    Here's a useful script that will automatically delete ALL user profiles that are on a Windows Server 2008/R2 computer.   WARNING! This is a dangerous operation, designed for use by an administrator who needs to do a complete purge; for instance a…
    This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
    This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

    746 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

    12 Experts available now in Live!

    Get 1:1 Help Now