Monday, March 12, 2012

How to create a Folder using a SQL Query?

Hi

How can I create a Folder (eg. C:\MyNewFolder) using SQL Query?

EXEC xp_cmdshell 'MD C:\MyNewFolder'

Chris

|||

hey ..u can create a file using FSO ..n operate further..

http://www.simple-talk.com/sql/t-sql-programming/reading-and-writing-files-in-sql-server-using-t-sql/

|||

hey chris .

i tried this in my query analyzer of sql2000 its not wrking..

the error is..

Could not find stored procedure 'xp_cmdshell'. even i tried using dbo.xp_cmdshell not wrking..

reply plz..

thanks

|||

All Extended Procedures reside in the master database, so try this:

EXEC master.dbo.xp_cmdshell 'MD C:\MyNewFolder'

Chris

|||

Chris Howarth wrote:

EXEC xp_cmdshell 'MD C:\MyNewFolder'

Chris

Thanks Chris

It work perfect, Im using SQL EXPRESS 2005

|||

Is there a way to do this?

Code Snippet

declare @.Location nvarchar(50)

set @.Location = 'C:\mynewfolder'

EXEC master.dbo.xp_cmdshell 'MD ' + @.Location

Im getting the following Error

Code Snippet

Msg 102, Level 15, State 1, Line 3

Incorrect syntax near '+'.

|||I tried your code in my SQL Server, I think he doesn't like the concatenation as a parameter...

try to set your string to a variable

Code Snippet

declare @.cmdpath nvarchar(60)
set @.cmdpath = 'MD '+ @.Location
exec master.dbo.xp_cmdshell @.cmdpath


Regards

|||

celobateira wrote:

I tried your code in my SQL Server, I think he doesn't like the concatenation as a parameter...

try to set your string to a variable

Code Snippet

declare @.cmdpath nvarchar(60)
set @.cmdpath = 'MD '+ @.Location
exec master.dbo.xp_cmdshell @.cmdpath


Regards

Its working, thank you

|||

Hi,

I added some code in order to get the result from the xp_cmdshell command

This returns null if successfull, if an error occurs returns the error message. May be useful instead of getting an sql error

Code Snippet

declare @.cmdpath nvarchar(60), @.Location nvarchar(100), @.message nvarchar(max)

set @.Location = N'C:\Temp\Temp5'

set @.cmdpath = 'MD '+ @.Location

Create table #result

(

result nvarchar(255)

)

insert into #result (result) exec master.dbo.xp_cmdshell @.cmdpath

select @.message = ISNULL(@.message + ' - ','') + result from #result where result is not null

select @.message

drop table #result

Eralper

http://www.kodyaz.com

|||

thxx chris.. its working ....

thx a lott

No comments:

Post a Comment