xp_cmdshell problem with path space

  • I am trying execute a program that has spaces in the path like this :

    EXEC xp_cmdshell '"C:\Program Files\try.exe" c:\ky\ky1.txt';

    I get the following error:

    " 'C:\Program' is not recognized as an internal or external command, "

    I tried to add double-quotes as above but it didn't work...Can some one show me how to traverse the path ?

  • You need to move the double quotes:

    EXEC xp_cmdshell '"C:\Program Files\try.exe c:\ky\ky1.txt"';

  • Use a single quote around the entire command and double quotes to delimit the start and end of strings.

    exec master.dbo.xp_cmdshell 'dir "c:\program files\*.*" '

    SQL = Scarcely Qualifies as a Language

  • Thank You

  • I have a similar problem :

    SET @command = ' "C:\Program Files (x86)\gs\gs8.64\bin>gswin32.exe" -q -sDEVICE=png16m

    -dBATCH -dNOPAUSE' +' -dFirstPage=1 -dLastPage=1 -r300 -sOutputFile=' + @OutFileName

    + ' ' + @InFileName

    EXEC xp_cmdshell @command

    'C:\Program' is not recognized as an internal or external command

    Not sure whats wrong here?

  • 'C:\Program' is not recognized as an internal or external command

    Not sure whats wrong here?

    I spent some time today trying to figure this out, as I had the same issue. Basically, you need something in front of your qualified path in xp_cmdshell.

    I was trying to qualify the version of dtexec.exe I was using (as I have both 2005 and 2008 on this server, leading to the wrong dtexec being picked up from the path environment variable). I changed my command to include a cd.. in front of the command.

    Old:

    exec xp_cmdshell '"D:\Program Files\Microsoft SQL Server\SQL 2005\100\DTS\Binn\dtexec.exe" ..... '

    This threw the same error you are getting.

    New:

    exec xp_cmdshell 'cd.. && "D:\Program Files\Microsoft SQL Server\SQL 2005\100\DTS\Binn\dtexec.exe" ..... '

    This did it. Just make sure your qualified path is not the first item in the command, and it works. Kinda stupid!

  • Hello,

    I had to have a look back and see how I sorted this out .. and here was my solution:-

    Basically I write the command line into a batch file, then exec the batch file, capturing any error.

    -- Create the command

    SET @command = 'echo "C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe" -q -sDEVICE=png16m -dBATCH -dNOPAUSE'

    + ' -r300 -dNOPROMPT -sOutputFile='

    + '"' + @OutFileName + '" "' + @InFileName + '" > ' + @cmdFileName

    SELECT @command

    -- Insert command into a batch/command file

    INSERT #result

    EXEC @ReturnCode = master.dbo.xp_cmdshell @command

    SET @ShellErrorMessage = ''

    SELECT @ShellErrorMessage = @ShellErrorMessage + SomeCol

    FROM #result

    WHERE SomeCol IS NOT NULL

    IF @ReturnCode <> 0

    RAISERROR (@ShellErrorMessage,16,1)

    DELETE #result

    -- Exec the batch file

    SET @command = @cmdFileName

    INSERT #result

    EXEC @ReturnCode = master.dbo.xp_cmdshell @command

    SET @ShellErrorMessage = ''

    SELECT @ShellErrorMessage = @ShellErrorMessage + SomeCol

    FROM #result

    WHERE SomeCol IS NOT NULL

    IF @ReturnCode <> 0

    RAISERROR (@ShellErrorMessage,16,1)

    DELETE #result

  • Worked for me. Thanks

  • You can do something like this too....

    DECLARE @p_cmd varchar(255)

    SET @p_cmd = 'cd "C:\Program Files\edb\mtk\bin" && runMTK.bat -dataOnly -tables table_changes -sourcedbtype sqlserver -targetdbtype postgresql -targetSchema public dbo'
    EXEC xp_cmdshell @p_cmd?

     

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply