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

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.

Who is Participating?
rettiseertConnect With a Mentor Commented:
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
Try removing the - before the 5
Bill PrewCommented:
You need some spaces.

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

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

aneuman2930Author Commented:
thanks - still no luck.  i already downloaded the forfiles resource kit, perhaps i have the wrong version.
aneuman2930Author Commented:
THANKS - this worked great - happy holidays
Bill PrewCommented:
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 /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

aneuman2930Author Commented:
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.
Bill PrewCommented:
No problem, just trying to help get you to a solution.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.