Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xp_cmdshell - How can I run multiple CMD commands with one script?

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello,

I am trying to use xm_cmdshell to create and populate a .txt file with the following data:
username
password
cd notindexed
put date.zip
bye

I can add multiple rows to my text file with the following:
<code>
exec master..xp_cmdshell '>c:\temp\ft.txt echo username'
exec master..xp_cmdshell '>>c:\temp\ft.txt echo password'
exec master..xp_cmdshell '>>c:\temp\ft.txt echo cd notindexed'
exec master..xp_cmdshell '>>c:\temp\ft.txt echo put date.zip'
exec master..xp_cmdshell '>>c:\temp\ft.txt echo bye'
</code>

However, is there a way I ran run it as one single script? I tried:
<code>
exec master..xp_cmdshell '>c:\temp\ft.txt echo username'
>>c:\temp\ft.txt echo password
>>c:\temp\ft.txt echo cd notindexed
>>c:\temp\ft.txt echo put date.zip
>>c:\temp\ft.txt echo bye'
</code>

and

<code>
declare @bigscript varchar(500)
set @bigscript =
'>c:\temp\ft.txt echo username'
>>c:\temp\ft.txt echo password'
>>c:\temp\ft.txt echo cd notindexed'
>>c:\temp\ft.txt echo put date.zip'
>>c:\temp\ft.txt echo bye'

print @bigscript
exec master..xp_cmdshell @bigscript
</code>

But both options only create my text file with the first line (uesrname).

This is a small example of a larger portion of my project so I hope there is a way to do this.

Thanks!

 
I just posted a simple example of a more complex thing I am trying to do. I am actually creating this file from variables in my database so I am hoping to do it through SQL. My existing batch file will then utilize this newly created ft.txt file.
 
Could you use BCP to create the .txt file?

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top