The forum will experience an outage sometime between February 10 at 7:00pm EST and February 12 at 11:59 EST for installation of security updates. The actual time and duration of the outage are unknown but attempts will be made to minimize the downtime. We apologize for any inconvenience.

# How to handle environment variables and pathes with spaces in xp_cmdshell calls?

 [A follow-up from Breck's comment on this question..., all tested with SA 12.0.0.2566.] In a CMD prompt, a simple call to DBTRAN a log file ``````"%SQLANY12%\bin32\dbtran" C:\abc.log > C:\test.txt 2> C:\test.err `````` works as expected - i.e. test.txt contains the output and test.err the (here expected) error that abc.log doesn't exist (in my case). The corresponding xp_cmdshell call shows that environment variables like SQLANY12 are recognized: ``````xp_cmdshell('"%SQLANY12%\bin32\dbtran" C:\abc.log > C:\test.txt 2> C:\test.err') `````` seems to replace the environment variable with the correct value. However, it does not seem to handle spaces in pathes correctly, as test.err txt contains the following error (translated from German): The command "C:\Programme\Sybase\SQL" ... could not be found. Note, the %SQLANY12% value contains spaces, and so I put double quotes around the path specification. This works in the CMD box but not in xp_cmdshell, no matter if I use double quotes or doubled single quotes around the path. In contrast, the corresponding call with the expanded path works as expected: ``````xp_cmdshell('"C:\Programme\Sybase\SQL Anywhere 12\bin32\dbtran" abc.log > C:\test.txt 2> C:\test.err') `````` Question: So how can environmnent variables with pathes including spaces be handled correctly? asked 21 Sep '10, 09:22 Volker Barth 29.3k●287●438●645 accept rate: 32% 2 The other conclusion: Pathes including spaces are inherently evil... (21 Sep '10, 09:23) Volker Barth That's why I always modify the default installation path to something like 'C:ProgrammeSybaseSQLAny11'. But with an english Windows version 'C:Program FilesSybaseSQLAny11' still contains a space ... (23 Sep '10, 15:15) Reimer Pods

 Are you using a shell replacement such as TCC or 4nt? Verify that COMSPEC is set to the cmd.exe that comes with Windows. Internally, xp_cmdshell will use %COMSPEC% or cmd.exe if COMSPEC is not set and build the following command line: %COMSPEC% /c user_cmdline and then passes it to CreateProcess. When I have comspec set to use TCC, I get the error you reported but when it is set to cmd.exe it works correctly. -john. answered 21 Sep '10, 14:41 John Smirnios 8.7k●3●77●106 accept rate: 40% No, I'm just using the original Windows cmd.exe. (21 Sep '10, 15:27) Volker Barth Reproduced the behaviour I mentioned on another box (Win XP2 SP3) with the default CMD.exe, i.e. COMSPEC=C:WindowsSystem32CMD.exe (that's what xp_cmdshell('SET COMSPEC > C:test.txt') does output, too). (21 Sep '10, 15:36) Volker Barth Interestingly enough, the "%COMSPEC% /c" call works correctly. Now I'm really puzzled. (21 Sep '10, 15:47) Volker Barth @All: Anyone else to confirm this behaviour? (23 Sep '10, 16:00) Volker Barth
 toggle preview community wiki:

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Question tags:

question asked: 21 Sep '10, 09:22

question was seen: 3,027 times

last updated: 21 Sep '10, 15:31