How do I run a batch file from a scheduled Job via SQL Server Agent

I'm attempting to run a (Windows) batch file from within a SQL Server agent job under SQL Server 2008 R2. In the Job Steps, I've created a new step, set its type to "Operating system (CmdExec)". I initially tried to put just the batch file name in the "Command" field. It gives an error that it can't find the file.

Then I tried: cmd.exe c:\test.bat

This starts, but never completes (and it doesn't look like it's actually working anyway).

I tried wrapping the batch file name in quotes: cmd.exe "C:\test.bat" but that also doesn't make any difference.

What is the magical combination to get this to work?

-
Were you using the complete path when you got the "file not found" error? Is the path valid on the executing server, not just your workstation? Are the paths within the batch file absolute? – Jon of All Trades Dec 13 '11 at 22:12
Yes, I'm using the full path. It is the correct path on the server running it. The batch file is nothing more than a simple test: echo > C:\temp\itworks.txt – Paul Mrozowski Dec 14 '11 at 12:45

If memory serves me correctly:

cmd.exe /c "c:\test.bat"


Quotes are needed if there are any spaces in the path or file name

-
Yep, that seems to work. – Paul Mrozowski Dec 14 '11 at 12:53

As well check the permissions used for the Job Agent Service. Could be that the user running the service does not have permission to see the file.

Good Luck

-

Check that you have permissions to run the batch file on that drive and folder location.

Don't forget when you run it manually your credentials are used but when it runs automatically by SQL Agent, the agent's credentials and permissions are used.

-
Why the downvote? – ivanmp Feb 18 '15 at 19:08