Wednesday, January 2, 2013

TSQL Make Directory

Create a directory using xp_cmdshell. Script includes steps to enable xp_cmdshell and then disabling it when finished. Take that out if you want.



--ENABLE xp_cmdshell to execute the mkdir
EXEC sp_configure 'show advanced options', 1
GO RECONFIGURE
GO EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
--Make the directory

DECLARE @cmd VARCHAR(100), 
       @directory VARCHAR(100) = 'C:tempjoe ' + @@SERVERNAME 
SET @cmd = ' mkdir ' + @directory
EXEC xp_cmdshell @cmd, no output

--Disable xp_cmdshell
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE
GO

0 comments:

Post a Comment