Schedule a batch file with parameters containing spaces

Posted by Danilo Brambilla on Server Fault See other posts from Server Fault or by Danilo Brambilla
Published on 2010-05-11T07:58:42Z Indexed on 2010/05/11 8:04 UTC
Read the original article Hit count: 266

Hi,

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

© Server Fault or respective owner

Related posts about scheduled-task

Related posts about windows-server-2003