Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 XP Command Shell

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-09-11 : 17:02:38
Hi,
I want to create a text file by using command shell and XP_CmdShell ESP
At command prompt (in XP) for creating a text file we can use:
C:\>copy con test.txt (ENTER)
Some text (ENTER)
^Z (ENTER)


I use following query:
execute xp_cmdshell 'copy con test.txt
test^Z'


but this query is not correct,
please post correct method/query.

thanks.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-09-11 : 17:08:00
execute xp_cmdshell 'echo test >>c:\test.txt'
execute xp_cmdshell 'echo more test data >>c:\test.txt'


You'll need a separate echo command for each line of text you want in the file. You cannot use the "copy con" method because it polls the keyboard.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-09-11 : 17:17:18
thank you,
May post an example for creating a text file with multiple lines?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-09-11 : 17:25:27
I did. You use the >> append operator to the same file name.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-09-11 : 17:45:58
I do not understand!
C:\>echo line1 >> line2 >> line3 >>t.txt
OR
C:\>echo line1 >>t.txt line2 >>t.txt line3>>t.txt

Result:
line1 line2 line3


C:\>echo line2>>t.txt
C:\>echo line2>>t.txt
C:\>echo line2>>t.txt

Result:
line1
line2
line3
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-11 : 17:57:35
He posted exactly what you need to run. You call the echo command with >> for each line you want. You do not put multiple >> in the same command line. Do just like he shows with xp_cmdshell.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-09-11 : 18:14:02
Okay now I understand, for creating multiple lines we use multi xp_cmdshell. I thought we can use only one command line.
Thanks
Go to Top of Page
   

- Advertisement -