Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

create directory in sql server

/*    
DECLARE @outPutPath2 NVARCHAR(500)    
EXEC sp_un_CreateDirectory 'folder1folder2folder3folder4', 'TEST01', @outPutPath2 OUTPUT       
EXEC sp_un_CreateDirectory @outPutPath2, 'TEST01', @outPutPath2 OUTPUT    
*/    
    
CREATE PROCEDURE sp_un_CreateDirectory(    
 @parentDirectory NVARCHAR(500)    
 ,@folderName NVARCHAR(200)    
 ,@outPutPath NVARCHAR(500) = NULL  OUTPUT    
)    
    
AS     
    
/*Enable the functionality of file processing      
    
EXEC master.dbo.sp_configure 'show advanced options', 1      
RECONFIGURE      
      
EXEC master.dbo.sp_configure 'xp_cmdshell', 1      
RECONFIGURE      
    
exec xp_cmdshell 'net use SICEMsharedData password /USER:julyservices
asel'    
      
*/     
    
DROP TABLE IF EXISTS #TempTableParentDir    
DROP TABLE IF EXISTS #TempTableDestinationDir    
    
DECLARE @SQLcmd NVARCHAR(500), @IsDesDirectory BIT, @DesParentDirectoryExists BIT;      
CREATE TABLE #TempTableParentDir (FilesExists BIT, IsDirectory BIT, ParentDirectoryExists BIT);      
CREATE TABLE #TempTableDestinationDir (FilesExists BIT, IsDirectory BIT, ParentDirectoryExists BIT);      
    
INSERT INTO #TempTableParentDir      
EXEC Master.dbo.xp_fileexist @parentDirectory     
    
SELECT @IsDesDirectory = IsDirectory ,@DesParentDirectoryExists = ParentDirectoryExists FROM #TempTableParentDir      
    
IF(@DesParentDirectoryExists = 0) RETURN;   
  
IF(@IsDesDirectory = 0 AND @DesParentDirectoryExists = 1)      
BEGIN  
 SET @SQLcmd = 'mkdir'+@parentDirectory;      
 EXEC MASTER..xp_cmdshell @SQLcmd      
END     
    
DECLARE @Inpath  VARCHAR(200)='';      
SET @Inpath = @parentDirectory+''+@folderName;      
  
print(2);    
INSERT INTO #TempTableDestinationDir      
EXEC Master.dbo.xp_fileexist @Inpath     
    
SELECT @IsDesDirectory = IsDirectory ,@DesParentDirectoryExists = ParentDirectoryExists FROM #TempTableDestinationDir      
IF(@DesParentDirectoryExists = 0) RETURN;    
IF(@IsDesDirectory = 0 AND @DesParentDirectoryExists = 1)      
BEGIN      
 SET @SQLcmd = 'mkdir'+@Inpath;      
 EXEC MASTER..xp_cmdshell @SQLcmd      
END     
    
DROP TABLE IF EXISTS #TempTableParentDir    
DROP TABLE IF EXISTS #TempTableDestinationDir    
    
SELECT @outPutPath =@Inpath    
    
RETURN    
 
PREVIOUS NEXT
Tagged: #create #directory #sql #server
ADD COMMENT
Topic
Name
5+7 =