all groups > sql server programming > january 2005 >
You're in the

sql server programming

group:

write multiple lines to DOS file with xp_cmdshell


write multiple lines to DOS file with xp_cmdshell Dave N
1/19/2005 10:13:02 PM
sql server programming:
I am trying to write multiple lines to a DOS file with xp_cmdshell. This
works:

DECLARE @cmd varchar(255)
SET @cmd = 'echo line 1 > C:\outfile.txt' -- overwrites the file if present
EXEC master..xp_cmdshell @cmd
SET @cmd = 'echo line 2 >> C:\outfile.txt' -- appends to the file
EXEC master..xp_cmdshell @cmd


Is there a way to somehow concatenate the lines into one string and call
xp_cmdshell once? I tried this, and it does not work:

DECLARE @cmd varchar(255)
SET @cmd = 'echo line 1' + CHAR(13) + CHAR(10) + 'echo line 2' >
C:\outfile.txt' -- overwrites the file if present
EXEC master..xp_cmdshell @cmd


Any ideas? I can write to a temp table, then bcp out, but that seems like
overkill to write just a few lines. Besides, the second method above really
oughta work.
Re: write multiple lines to DOS file with xp_cmdshell oj
1/19/2005 10:59:15 PM
exec master..xp_cmdshell N'echo test1>c:\test.txt & echo test2>>c:\test.txt'



--
-oj


[quoted text, click to view]

Re: write multiple lines to DOS file with xp_cmdshell Dave N
1/20/2005 7:55:04 AM
Thanks, I'll give that a try. But is there a way to escape the control
characters <CR><LF> so that my original method will work?

[quoted text, click to view]
Re: write multiple lines to DOS file with xp_cmdshell oj
1/20/2005 9:17:05 AM
You're invoking 2 echo commands; thus, the "&" is there to concatenate them.

--
-oj


[quoted text, click to view]

Re: write multiple lines to DOS file with xp_cmdshell Dave N
1/20/2005 10:43:04 AM
Got it working. Thanks.

[quoted text, click to view]
AddThis Social Bookmark Button