SQLTeam.com | Weblogs | Forums

Executing .exe using xp_cmdshell Not Working


Hi Guys,

Trying to execute an .exe using xp_cmdshell , it runs the exe.
But my issue is the following, the exe has to be the same folder as a .txt file as the exe outputs to the txt file.

If i double click the exe the output goes to the .txt file. I also created a .Bat file that executes the .exe and it outputs
to the .txt file.

If i try and run either the .exe or the .Bat files from SQL they run but it doesn't output to the file.
EXECUTE xp_cmdshell '"D:\Run.BAT"'
EXECUTE xp_cmdshell "D:\Application.exe"

In my Results i get the following from running the .Bat
C:\Windows\system32>cd D:\


Is SQL on the same server? In other words, D: is seen by SQL as the D: drive on it's host server.


Yes, its on the same server, D is the second drive


Sorry that was my only idea. Maybe someone else can help.


Just a thought:

Add a line to the top of the BATch file that changes drive / folder to the "root" where the EXE and TXT file are? Then the EXE will run "in" that folder.

THE CD (or CHDIR if you prefer) command has an optional "/D" parameter which will change drive as well as folder.


Hi Thanks for the response,

I have tried CHDIR /D D:\Application.exe
And CD /D D:\Application.exe

In both cases the bat file executes, but it doesn't output to the .txt file, is there something i am doing wrong in my bat file?

Seems like it isn't running the exe file.


CD requires just a path (and optional Drive too).

Put the EXEcutable command on a separate like

CD /D D:\


CD /D D:\MyPath


Kristen, thank you soooooo much.

It worked like a charm. Made my entire week now that this works