Delete x days of files Windows Server 2003 SP2

555 pts.
Tags:
Batch files
Windows Server 2003
Windows Server 2003 SP2
I am trying to use ForFiles to delete files in a folder that contains files older than 30 days. I am using Windows 2003 server. This is what I'm getting on the DOS screen:

C:temp>Windows 2003 u Forfiles 'Windows' is not recognized as an internal or external command, operable program or batch file. C:temp>ECHO **** DELETING FILES **** C:temp>forfiles /p c:test072909 /s /m *.* /d -30/c "cmd /c del echo @file" ERROR: Invalid argument/option - 'cmd /c del echo @fiile'.Type "FORFILES /?" for usage. 


This is in my batch script:

Windows 2003 – ForFiles ECHO ***** DELETING FILES ***** forfiles /p c:test072909 /s /m *.* /d -10/c "cmd /c del echo @file" pause



Software/Hardware used:
Windows 2003 SP2

Answer Wiki

Thanks. We'll let you know when a new response is added.

First, remove the “Windows 2003 – ForFiles” line from your script.

Then, try changing your forfiles call to this:

<pre>forfiles /p c:\test072909\ /s /m *.* /d -30 /c "cmd /c del @file"</pre>

If you just want to list the files, then use this:

<pre>forfiles /p c:\test072909\ /s /m *.* /d -30 /c "cmd /c echo @file"</pre>

A blank space was missing before “/c”, and you were trying to execute two commands (del and echo).

Discuss This Question: 11  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Kwt712
    Great, it worked thanks for the help. I do have one question. Is there a way to download ForFiles to make it work on WinXP Professional SP2. I cannot have the same script work off of my desktop. Any recommendations. Thanks
    555 pointsBadges:
    report
  • Kwt712
    ok, i have a problem now, i just put this on a production server Windows 2003 SP2 ... this is what i am getting now when i run a script against the foler C:Batch File>ECHO ***** Files1F:userDischarge forms - savedCompleted ***** ***** Files1F:userDischarge forms - savedCompleted ***** C:Batch File>ECHO ***** DELETING FILES ***** ***** DELETING FILES ***** C:Batch File>forfiles /p "F:userDischarge forms - savedtest" /s /m *.* /d - 25 /c "cmd /c del @file" ERROR: Invalid argument/option - '@file'. Type "FORFILES /?" for usage. C:Batch File>pause Press any key to continue . . . I have 2 folders one is completed that the script will run against and the other one is test with same files in them. I have files dated 7/11/2009 and the script will not clean them up. where did i go wrong here now. thanks
    555 pointsBadges:
    report
  • carlosdl
    Can you post the contents of your script file ? (not the results when running it) Thanks,
    69,175 pointsBadges:
    report
  • Kwt712
    here you go ECHO ***** Files1F:userDischarge forms - savedCompleted ***** ECHO ***** DELETING FILES ***** forfiles /p "F:userDischarge forms - savedtest" /s /m *.* /d -25 /c "cmd /c del @file" pause ALSO, is it possible to make ForFiles to work with XP. If yes, then how. Thanks
    555 pointsBadges:
    report
  • carlosdl
    The problem seems to be the double quotes (") in the path. You could try changing directory first, and then executing forfiles. Something like this:
    F:
    cd "F:userDischarge forms - savedtest"
    forfiles /s /m *.* /d -25 /c "cmd /c del @file"
    69,175 pointsBadges:
    report
  • carlosdl
    There is a version you can download that runs on XP, but it is not the same version as the one included in Server 2003 or 2008, and the syntax is not the same. You can download it from here. Basically, you use "-" instead of "/" to specify the parameters, and you can't have blank spaces between the parameter option and the parameter itself. The @file variable must be in Uppercase. You would need to change your script to something like this:
    cd "F:userDischarge forms - savedtest"
    forfiles -s -m*.* -d-25 -c"cmd /c del @FILE"
    69,175 pointsBadges:
    report
  • Kwt712
    The ForFiles for Windows XP. It is only 12kb. Does that file needs to be in a certain directory for it to work. thanks
    555 pointsBadges:
    report
  • carlosdl
    Yes, it is a small program. It should be in a directory included in your PATH environment variable, for example "C:windowssystem32"
    69,175 pointsBadges:
    report
  • Kwt712
    can you tell me where exactly i need to put this file, i already copy it in C:windowssystem32 folder. Let me know. thanks
    555 pointsBadges:
    report
  • carlosdl
    and ... did it work ? You can put it wherever you want, but you have to make sure that path is included in your PATH environment variable.
    69,175 pointsBadges:
    report
  • Kwt712
    ok let me get this straight ... under Environment Variable, i have a User Variables and System Variables ... i am guessing that the forfiles will be under user variables as C:windowssystem32forfiles.exe let me know if i have this right. thanks
    555 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following