Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to schedule a task in Windows Server 2003 that executes this script that deletes files older that n days in the specified folder. The script needs 3 parameters:

%1 path to folder where files need to be deleted
%2 file names (es. *.log)
%3 number of days 

@echo off
forfiles -p %1 -s -m %2 -d -%3 -c "cmd /c del /q @path"

The script works fine if the first parameter has no spaces inside. This is an example of parameters that work:

"C:\Program Files\SCRIPT\DeleteFilesOlderThanXDays.cmd" N:\FOLDER\FOLDER *.zip 60

This is an example that does not work:

"C:\Program Files\SCRIPT\DeleteFilesOlderThanXDays.cmd" N:\Program Files\LOG *.zip 60

This does not work too:

"C:\Program Files\SCRIPT\DeleteFilesOlderThanXDays.cmd" "N:\Program Files\LOG" *.zip 60

I think it would be a quotes problem but I can't figure out the solution. I'd like not to insert values directly into the script if possible

Thank you all for help

share|improve this question
do you get any error messages/etc. that indicate what the problem is? That should help to debug. The 3rd command line you posted (where the path is double-quoted) should be working. – MattB May 11 '10 at 15:34

Not a full solution, but a workaround that might get your task working until you find a better resolution: you could try calling a batch file that does not take parameters and does nothing but call "C:\Program Files\SCRIPT\DeleteFilesOlderThanXDays.cmd" "N:\Program Files\LOG" *.zip 60

share|improve this answer
up vote 0 down vote accepted

Thank you for your answers. After further investigations it comes out that the batch file should be modified adding quotes around @path to allow spaces:

@echo off forfiles -p %1 -s -m %2 -d -%3 -c "cmd /c del /q "@path""

The path parameter has to be quoted too as follows because of spaces:

"C:\Program Files\SCRIPT\DeleteFilesOlderThanXDays.cmd" "N:\Program Files\LOG" *.zip 60

Now, it does not work too because having "C:\Program Files\SCRIPT\DeleteFilesOlderThanXDays.cmd" followed by a quoted parameter fails with an error like "C:\Program is not recognized as an internal or external command..." so no luck again.

Workaround that works:

"C:\Program Files\SCRIPT\DeleteFilesOlderThanXDays.cmd" . *.zip 60

Start in: "N:\Program Files\LOG"

share|improve this answer

In your batch file add quotes around the parameters that can include spaces. So, at least around %1.

@echo off forfiles -p "%1" -s -m %2 -d -%3 -c "cmd /c del /q "@path""
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.