xp_cmdshell

  • Saw the artical on Using xp_cmdshell. I am trying to use it to call an external exe, but the path contains space, anyone has any idea's how to get around this. Thanks very much for your help!

  • try like this...

    exec xp_cmdshell'dir D:\"Program Files"\ > d:\d_200304021.txt'

    .

  • I think it should be like this:

    exec xp_cmdshell'dir "D:\Program Files\" > d:\d_200304021.txt'

    Of course, to be sure you could just open a prompt and run tyhe dir-command (or whatever command you are trying) there and make sure it works.

    --

    Chris Hedgate @ Apptus Technologies (http://www.apptus.se)

    http://www.sql.nu

  • That works for space in path, but I just realize new problem. I need to use a variable in the xp_cmdshell, something like the following:

    declare @filename varchar(50)

    DECLARE @cmd varchar(100)

    set @filename = 'nomenclature_file.xml'

    SET @cmd = 'c:\test_tmp\"folder tooo test"\mdm_CleanExternalXML.exe ' + @filename

    EXEC master..xp_cmdshell @cmd

    I know I can't put double quote inside string assignment, but I can assemble the line in xp_cmdshell line, cause it does not recognize the + sign. Any idea how to do this? Thanks very much

  • quote:


    declare @filename varchar(50)

    DECLARE @cmd varchar(100)

    set @filename = 'nomenclature_file.xml'

    SET @cmd = 'c:\test_tmp\"folder tooo test"\mdm_CleanExternalXML.exe ' + @filename

    EXEC master..xp_cmdshell @cmd


    Actually xp_cmdshell will accept one set of double quotes within the command string, so you can account for spaces in the the filename. Try using this on your second SET statement:

    SET @cmd = '"c:\test_tmp\folder tooo test\mdm_CleanExternalXML.exe" ' + @filename

    That's a single quote followed by a double quote at the beginning of the string.

    -SJT

    Edited by - TheWildHun on 04/03/2003 12:56:58 PM

    Edited by - TheWildHun on 04/03/2003 12:58:36 PM

  • what if I have more than one directory have spaces? Any idea? Not trying to be difficult, that is the real problem. Thanks!

  • I know BOL says that only one set of double quotes are allowed, but I just tested it with two sets and it worked fine. Here's what I tested:

    
    

    declare @cmd varchar(500)
    set @cmd = 'dir "C:\Documents and Settings\Administrator\My Documents\*.doc"'
    set @cmd = @cmd + ' > "C:\Documents and Settings\Administrator\My Documents\docdir.txt"'
    exec master..xp_cmdshell @cmd

    Jay Madren


    Jay Madren

Viewing 7 posts - 1 through 6 (of 6 total)

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