0

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

MattB
  • 11,194
  • 1
  • 30
  • 36
Danilo Brambilla
  • 1,031
  • 2
  • 15
  • 33
  • 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

3 Answers3

1

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

David Spillett
  • 22,754
  • 45
  • 67
0

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"

Danilo Brambilla
  • 1,031
  • 2
  • 15
  • 33
0

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""
Scott Pack
  • 14,907
  • 10
  • 53
  • 83
Niels
  • 1