Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following in a batch file that works perfectly fine if I execute it manually but fails when I tell the task scheduler to run it.

forfiles -s -m *.bak -d -4 -c "cmd /c del @path"

This batch file deletes any BAK files in the same directory as the batch that are older than 4 days. I couldn't get the forfiles to work from another directory so the batch file only works within the same directory as my sql backups. If anyone knows how to edit my batch file to allow me to be able to call it from my common batch files directory instead of my sql backups folder that info would be much appreciated.

Even when I check the "run with highest priviledges" checkbox when configuring the scheduled task there is no change. Btw, my task only runs when the user is logged in and the user account is actually an administrator and UAC is fully disabled on this machine. So I'm not sure how this could be a permission related issue but my gut is pointing me to that as a cause. I will hopefully be able to edit and include the error message displayed in the batch when the task scheduler calls it to run. I need to edit the batch so it doesn't close before I can see the error details and currently the machine that holds the file is in use by another developer so I will have to gather that additional info later.

Hope someone can help! This machine runs sql express and therefore can't use maintenance plans so this is my workaround. If there's an easier way to do what i'm trying to do let me know.

share|improve this question
up vote 1 down vote accepted

Did you try using /p to search a different path?

forfiles /p "d:\path\to\sql\backups" /s /m *.bak /d -4 /c "cmd /c del @path"

Also, compare the user account that the task runs as with security settings on the SQL backups folder.

share|improve this answer
Wow that was fast. Thanks! You solved both my problems. I had tried using the P switch with forfiles but I guess I read the syntax example wrong. It did turn out to be permission issue even though "Administrators" group had full access over the folder. Another security definition for the specific account I was logged in under was necessary with full control as well. After that it all started working like I imagined. – TWood Apr 14 '11 at 21:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.