1

This question has been asked before and I am referencing the same answer as Suggested here. But the thing is that this is working fine in windows 7 ultimate whereas, the same code is not working correctly in windows server 2008 R2. It deletes all the text files from the folder if it found any of the file older than specific date, whereas it doesn't do anything if it doesn't found any text file older than specific date which is correct. The command is as follows;

REM Remove text files older than 5 days
forfiles -p "E:\Backups" -s -m *.* -d -5 -c "cmd /c del E:\Backups\*.txt"

I have seen other posts as well but I couldn't find this particular problem in them. Please suggest the solution if somebody has faced this before or if I am doing something wrong here, Thanks.

Community
  • 1
  • 1
Learner
  • 776
  • 6
  • 14
  • 40

1 Answers1

6

Replace E:\Backups\*.txt with @path.

forfiles -p "E:\Backups" -s -m *.* -d -5 -c "cmd /c echo del @path"

When you're satisfied that does what you want, remove the echo. See forfiles /? for full details of the @variables available within the command. The documentation for @path is a little confusing. @path returns the fully qualified drive:\path\to\filename.ext, already quoted.

rojo
  • 24,000
  • 5
  • 55
  • 101