Need help with echo in XP_cmdshell

  • Hi all,

    I'm having a hard time with the following:

    I'm creating a batch file with XP_cmdshell that contains a list of Dos commands which will end up being run with XP_cmdShell.

    Here's the example I have. (wh is a program that we use)

    select@sql = 'echo wh '+ @path +@Create + ' > ' +@logdir + '\'+@logname+

    '_Create'+ replace(replace(replace(convert(varchar(20),getdate(),20),':',''),' ','' ),'-','')+ '.txt 2>&1'

    + ' >> ' + @workfilename

    print @sql

    exec master..xp_cmdshell @sql

    The print Statement gives the following:

    echo wh Validation\CSTX\create_cstx.txt > Validation\Logs\cstx_Create20080808132759.txt 2>&1 >> c:\temp\Validate.bat

    What I'm trying to do is put the WH command (up until and Including '2>&1') into the Batch file. Obviously when running the XP_Cmdshell it stops at the first '>'... so the batch file contains:

    wh Validation\CSTX\create_cstx.txt

    What do I need to do so that XP_cmdshell includes the 2 '>' as part as the echo command and append it to the Validate.bat file?

    I've tried the ^, \0076 and /0076 escape characters without succeeding!

    Any help would be greatly appreciated.

    Thank You

    John

  • Wow!!! I found it!!! (3 hours later)

    The answer for those interested is to put the escape character ^ before the 2 first <.

    and also need to put an escape character before the & character (which I had missed)

    :w00t::hehe::D

  • It was a long time ago, but, if possible, could you share the working line (code). Where and in how many places did you put the escape character ^? Having similar issues with my code.

    Thanks

    Jd

Viewing 3 posts - 1 through 2 (of 2 total)

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