0

I am trying to delete all folders and files on a nightly basis using task scheduler. I am using the following command in the actions:

Program/Script: forfiles
Arguments: /S /P C:\Users\david\AppData\Roaming\Apple Computer\MobileSync\Backup /C "cmd /c if @isdir==TRUE rd /s /q @file"

This is not deleting any file or folder. What do i have missing in this example?

This seems to work but does throw an error: /S /P "C:\Users\Sales\AppData\Roaming\Apple Computer\MobileSync\Backup" /C "cmd /c rd /s /q @file

Error:

C:\Users\david>forfiles /S /P "C:\Users\david\AppData\Roaming\Apple Computer\MobileSync\Backup"  /C "cmd /c rd /s /q @file

ERROR: The system cannot find the file specified.

C:\Users\david>
1
  • Ill add as well that the process just stays running with no output May 30, 2019 at 14:09

1 Answer 1

0

Why are you searching subdirectories when you plan on clobbering everything with "rd /s /q"?

What you might (are probably) be seeing is this:

  1. forfiles builds a list of files to work with: say... "C:\This" and "C:\This\That"
  2. You get "C:\This" handed to you
  3. You delete "C:\This" recursively
  4. You get "C:\This\That" handed to you.
  5. You get an error deleting it because it doesn't exist.

One other condition can happen too! There can be a file handle open while enumerating the tree so when you try to delete "C:\This", "C:\This\That" is still locked open by forfiles. I don't know if forfiles is written to circumvent this but I have for sure run into this while programming.

Suggestion: Drop the /S from forfiles (best) OR add "blah blah blah.. if exist @file" blah blah blah (not best).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .