SQLTeam.com | Weblogs | Forums

Executing .exe using xp_cmdshell Not Working


#1

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
NULL
C:\Windows\system32>cd D:\
NULL
C:\Windows\system32>Application.exe
NULL


#2

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


#3

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


#4

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


#5

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.


#6

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.


#7

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

Put the EXEcutable command on a separate like

CD /D D:\
Application.EXE

or

CD /D D:\MyPath
Application.EXE

#8

Kristen, thank you soooooo much.

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