[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-12-23
8
Medium Priority
?
284 Views
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.


0
Comment
Question by:aneuman2930
8 Comments
 
LVL 8

Expert Comment

by:jtdebeer
ID: 26114122
Try removing the - before the 5
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26114132
You need some spaces.

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

~bp
0
 
LVL 13

Accepted Solution

by:
rettiseert earned 2000 total points
ID: 26114175
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
            File.Delete
        End If
    Next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:aneuman2930
ID: 26114177
thanks - still no luck.  i already downloaded the forfiles resource kit, perhaps i have the wrong version.
0
 

Author Closing Comment

by:aneuman2930
ID: 31669509
THANKS - this worked great - happy holidays
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26114208
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.

http://home.mnet-online.de/horst.muc/wbat32.htm#top

~bp
FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]
 
Description:
    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
                        quotes.
 
                        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
                                   extension.
                        @ext     - returns only the extension of the
                                   file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                   file.
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                                   bytes.
                        @fdate   - returns the last modified date of the
                                   file.
                        @ftime   - returns the last modified time of the
                                   file.
 
                        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.
 
Examples:
    FORFILES /?
    FORFILES
    FORFILES /P C:\WINDOWS /S /M DNS*.*
    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

0
 

Author Comment

by:aneuman2930
ID: 26114225
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.
0
 
LVL 59

Expert Comment

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

~bp
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

834 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