15

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?

2
  • 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? Commented Dec 13, 2011 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 Commented Dec 14, 2011 at 12:45

4 Answers 4

15

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

1
  • 1
    If working directory is needed for script, you may need something like: cmd.exe /c "cd /d c:\temp && c:\temp\test.bat"
    – sonyisda1
    Commented Feb 8, 2021 at 0:36
2

only "c:\test.bat" will do the job

1
  • 2
    Hi, Welcome to the site. While your answer may be useful it's usually better to include some more details as to why this would work while the command in the question doesn't.
    – Tom V
    Commented Aug 22, 2019 at 9:34
1

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

1

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.

1
  • also have to set permission on files that the batch manipulates
    – robotik
    Commented Jul 5, 2021 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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