Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I just wanted to say that you guys RULE, a million thank you's to whoever created, and/or manages this site. KEEP UP THE GOOD WORK..."

Geography

Where in the world do Tek-Tips members come from?

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

bmacbmac (IS/IT--Management)
7 Jun 12 18:50
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!

markros (Programmer)
7 Jun 12 23:55
May be you can create a bat file? Also, why do you need to do this from SQL Server?

PluralSight Learning Library

bmacbmac (IS/IT--Management)
8 Jun 12 10:53
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.
djj55 (Programmer)
8 Jun 12 13:22
Could you use BCP to create the .txt file?

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close