Purging files using forfiles in a batch and running that batch from a task

batchtask-scheduler

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.

Best Answer

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.