BAT file will not run from Task Scheduler but will from Command Line

I'm trying to run a BAT script from Task Scheduler in Windows 2008 R2 and it runs for 3 seconds and then stops.

It says it successfully completes but I know it doesn't.

I can run this script from the command line directly, and it runs just fine.

The bat file I'm running actually deletes files older than 7 days using "forfiles" then I'm mapping a network drive, moving the files across the network using robocopy, and then closing the network connection.

I have taken the network and copy options out of the file and it still does the same thing. Here is how my file looks:

rem This will delete the files from BBLEARN_stats
forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_stats" -m *.* -d -17 -c "cmd /c del @file"

rem This will delete the files from BBLEARN_cms_doc
forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_cms_doc" -m *.* -d -14 -c "cmd /c del @path"

rem This will delete the files from BBLEARN_admin
forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_admin" -m *.* -d -10 -c "cmd /c del @path"

rem This will delete the files from BBLEARN_cms
forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_cms" -m *.* -d -10 -c "cmd /c del @path"

rem This will delete the files from attendance_bb
forfiles -p "E:\BB_Maintenance_Data\DB_Backups\attendance_bb" -m *.* -d -10 -c "cmd /c del @path"

rem This will delete the files from BBLearn
forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN" -m *.* -d -18 -c "cmd /c del @path"

rem This will delete the files from Logs
forfiles -p "E:\BB_Maintenance_Data\logs" -m *.* -d -10 -c "cmd /c del @path"

NET USE Z: \\10.20.102.225\coursebackups\BB_DB_Backups /user:cie oly2008

ROBOCOPY E:\BB_Maintenance_Data Z: /e /XO /FFT /PURGE /NP /LOG:BB_DB_Backups.txt

openfiles /disconnect /id *

NET USE Z: /delete /y

This is happening on 2 servers when trying to run commands from inside a BAT file.

The other server is giving an error if (0xFFFFFFFF) but that file is running a CALL C:\dir\dir\file.bat -options and I've used commands like that before in Server 2003.

Here is the file for this file:

call C:\blackboard\apps\content-exchange\bin\batch_ImportExport.bat -f backup_batch_file.txt -l 1 -t archive

NET USE Z: \\10.20.102.225\coursebackups\BB_Course_Backups /user:cie oly2008

ROBOCOPY E:\ Z: /move /e /LOG+:BB_Move_Course_Backups.txt

openfiles /disconnect /id *

NET USE Z: /delete /y

If batch files are failing to execute as a scheduled task but running fine interactively, it most probably is due to a security / permissions issue. The SYSTEM / LocalSystem account which is often used to run scheduled tasks usually does not have permissions to access any networked resources for example.

What is the security context of your scheduled task? You could try changing it to something more privileged (like a domain administrator account) temporarily as a hypothesis test.

If it works, you should create a new account with a complex password and sufficient privileges to use with tasks. The account password is saved along with the scheduled task - by using a separate account you are following a good security practice and preventing failing task runs whenever the administrator's password changes.