Schedule a batch file with parameters containing spaces

batch-filescheduled-taskwindows-server-2003

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

Best Answer

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

Related Topic