How do i delete a file in a specific folder older and 5 days?

Posted on 2009-12-23
Last Modified: 2012-05-08
I know this is an old question, but I am having a very tough time getting this to work.  
I would like to schedule a task on a windows 2000 server to delete files older than 5 days, within the c:\test directory:

I tried this batch file so far:
Forfiles -pC:\test -s -m*.* -d-5 -c "cmd /c del /q @path"

This really isn't my area, so please be a simple as possible. thank you.

Question by:aneuman2930
    LVL 8

    Expert Comment

    Try removing the - before the 5
    LVL 51

    Expert Comment

    by:Bill Prew
    You need some spaces.

    forfiles -p C:\test -s -m *.* -d -5 -c "cmd /c del /q @path"

    LVL 13

    Accepted Solution

    Or try this script, just change the first line and save it in a text file wth extension vbs

        Folder = "C:\T"
        Set fso = CreateObject("Scripting.FileSystemObject")
        For Each File In fso.GetFolder(Folder).Files
            If File.DateLastModified < Now() - 5 Then
            End If

    Author Comment

    thanks - still no luck.  i already downloaded the forfiles resource kit, perhaps i have the wrong version.

    Author Closing Comment

    THANKS - this worked great - happy holidays
    LVL 51

    Expert Comment

    by:Bill Prew
    Try it just from the command line first, as a test of the syntax.

    Are you getting any kind of error?

    If you aren't opposed to a utility to do the work, here's another good tool, DELAGE32.

    FORFILES [/P pathname] [/M searchmask] [/S]
             [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]
        Selects a file (or set of files) and executes a
        command on that file. This is helpful for batch jobs.
    Parameter List:
        /P    pathname      Indicates the path to start searching.
                            The default folder is the current working
                            directory (.).
        /M    searchmask    Searches files according to a searchmask.
                            The default searchmask is '*' .
        /S                  Instructs forfiles to recurse into
                            subdirectories. Like "DIR /S".
        /C    command       Indicates the command to execute for each file.
                            Command strings should be wrapped in double
                            The default command is "cmd /c echo @file".
                            The following variables can be used in the
                            command string:
                            @file    - returns the name of the file.
                            @fname   - returns the file name without
                            @ext     - returns only the extension of the
                            @path    - returns the full path of the file.
                            @relpath - returns the relative path of the
                            @isdir   - returns "TRUE" if a file type is
                                       a directory, and "FALSE" for files.
                            @fsize   - returns the size of the file in
                            @fdate   - returns the last modified date of the
                            @ftime   - returns the last modified time of the
                            To include special characters in the command
                            line, use the hexadecimal code for the character
                            in 0xHH format (ex. 0x09 for tab). Internal
                            CMD.exe commands should be preceded with
                            "cmd /c".
        /D    date          Selects files with a last modified date greater
                            than or equal to (+), or less than or equal to
                            (-), the specified date using the
                            "MM/dd/yyyy" format; or selects files with a
                            last modified date greater than or equal to (+)
                            the current date plus "dd" days, or less than or
                            equal to (-) the current date minus "dd" days. A
                            valid "dd" number of days can be any number in
                            the range of 0 - 32768.
                            "+" is taken as default sign if not specified.
        /?                  Displays this help message.
        FORFILES /?
        FORFILES /S /M *.txt /C "cmd /c type @file | more"
        FORFILES /P C:\ /S /M *.bat
        FORFILES /D -30 /M *.exe
                 /C "cmd /c echo @path 0x09 was changed 30 days ago"
        FORFILES /D 01/01/2001
                 /C "cmd /c echo @fname is new since Jan 1st 2001"
        FORFILES /D +12/23/2009 /C "cmd /c echo @fname is new today"
        FORFILES /M *.exe /D +1
        FORFILES /S /M *.doc /C "cmd /c echo @fsize"
        FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

    Open in new window


    Author Comment

    Thanks Bill - i will test this in a bit - i am going with the vb script right now as i am in a bit of a rush and need to close this out.   thanks again for the quick response.
    LVL 51

    Expert Comment

    by:Bill Prew
    No problem, just trying to help get you to a solution.


    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!

    Join & Write a Comment

    Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now