Search
 
SCRIPT & CODE EXAMPLE
 

SQL

move files from one folder to another in sql server

/*      
EXEC sp_un_FileTransfer 'Copy', 'SICEMsharedDataRasel', 'SICEMsharedDataRasel01Rasel02','Test007.txt',0,0,0     
*/       
CREATE PROCEDURE [dbo].[sp_un_FileTransfer](    
@transferType VARCHAR(20)    
,@sourcePathDirectory NVARCHAR(250)    
,@destinationPathDirectory NVARCHAR(250) = NULL   
,@fileName VARCHAR(200) = NULL    
,@isCopyAllFiles BIT = 0    
,@isMoveAllFiles BIT = 0    
,@isDeleteAllFiles BIT = 0    
)    
    
AS     
    
/*Enable the functionality of file processing    
  
EXEC xp_cmdshell 'dir *.exe';   
xp_cmdshell 'whoami.exe'    
  
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:domainusername'  
    
*/   
  
  
DECLARE @SQLcmd NVARCHAR(500),@Result int=0;    
    
DECLARE @IsDesDirectory BIT, @DesParentDirectoryExists BIT,    
 @IsSourceDirectory BIT, @SourceParentDirectoryExists BIT;    
    
CREATE TABLE #TempTableSourceDir (FilesExists BIT, IsDirectory BIT, ParentDirectoryExists BIT);    
CREATE TABLE #TempTableDestinationDir (FilesExists BIT, IsDirectory BIT, ParentDirectoryExists BIT);    
    
-- Source Directory     
INSERT INTO #TempTableSourceDir    
EXEC Master.dbo.xp_fileexist @sourcePathDirectory    
SELECT @IsSourceDirectory = IsDirectory ,@SourceParentDirectoryExists = ParentDirectoryExists FROM #TempTableSourceDir    
    
IF(@IsSourceDirectory = 0) RETURN;    
    
-- Destination Directory    
INSERT INTO #TempTableDestinationDir    
EXEC Master.dbo.xp_fileexist @destinationPathDirectory    
SELECT @IsDesDirectory = IsDirectory ,@DesParentDirectoryExists = ParentDirectoryExists FROM #TempTableDestinationDir    
    
IF(@DesParentDirectoryExists = 0) RETURN;    
    
-- Create new directory    
IF(@IsDesDirectory = 0 AND @DesParentDirectoryExists = 1)    
BEGIN    
 SET @SQLcmd = 'mkdir'+@destinationPathDirectory;    
 EXEC MASTER..xp_cmdshell @SQLcmd    
END    
    
-- Move single file    
IF(@isCopyAllFiles = 0 AND @isMoveAllFiles = 0 AND @isDeleteAllFiles = 0)    
BEGIN    
     
 DECLARE @sourceFilePath VARCHAR(200)= @sourcePathDirectory+''+@fileName;    
 DECLARE @FilesExists BIT;    
    
 CREATE TABLE #TempTableDir (FilesExists BIT, IsDirectory BIT, ParentDirectoryExists BIT);    
 INSERT INTO #TempTableDestinationDir    
 EXEC Master.dbo.xp_fileexist @sourceFilePath    
    
 SELECT @FilesExists = FilesExists FROM #TempTableDestinationDir    
    
 IF(@FilesExists = 0) RETURN;    
    
 DECLARE @Inpath  VARCHAR(200)='';    
 SET @Inpath = @sourcePathDirectory+''+@fileName;    
    
 IF(@transferType = 'Move')    
 BEGIN      
  SET @SQLcmd = 'Move '+@Inpath+' ' +@destinationPathDirectory     
  EXEC MASTER..xp_cmdshell @SQLcmd--, NO_OUTPUT    
 END    
    
 IF(@transferType = 'Copy')    
 BEGIN     
  SET @SQLcmd = 'COPY '+@Inpath+' ' +@destinationPathDirectory     
  EXEC MASTER..xp_cmdshell @SQLcmd--, NO_OUTPUT    
 END    
    
 IF(@transferType = 'Delete')    
 BEGIN    
  SET @SQLcmd = 'del'+@Inpath    
  EXEC MASTER..xp_cmdshell @SQLcmd--, NO_OUTPUT    
 END    
    
 DROP TABLE IF EXISTS #TempTableDir    
END    
    
IF(@isMoveAllFiles = 1)    
BEGIN    
 SET @SQLcmd = 'Move '+@sourcePathDirectory+' ' +@destinationPathDirectory     
 EXEC MASTER..xp_cmdshell @SQLcmd--, NO_OUTPUT    
END    
    
IF(@isCopyAllFiles = 1)    
BEGIN    
 SET @SQLcmd = 'Copy '+@sourcePathDirectory+' ' +@destinationPathDirectory     
 EXEC MASTER..xp_cmdshell @SQLcmd--, NO_OUTPUT    
END    
    
IF(@isDeleteAllFiles = 1)    
BEGIN    
    
 SET @SQLcmd = 'del ' + @sourcePathDirectory + ' /Q' -- Delete       
 EXEC MASTER..xp_cmdshell @SQLcmd--, NO_OUTPUT    
END    
 
DROP TABLE IF EXISTS #TempTableDestinationDir    
DROP TABLE IF EXISTS #TempTableSourceDir    
Comment

PREVIOUS NEXT
Code Example
Sql :: how to casting data types in postgresql 
Sql :: create a PostgreSQL user django on mac 
Sql :: unique in sql server 
Sql :: how to select month from date in sql 
Sql :: convert rows into columns in oracle 
Sql :: mysql find db contarint 
Sql :: split string by comma in sql server 
Sql :: truckat table mysql 
Sql :: sql unique select 
Sql :: sql compiler online 
Sql :: connect to mysql c# connection string C# 
Sql :: bigquery timestamp 
Sql :: postgresql create table many-to-many 
Sql :: using distinct and count together in sql 
Sql :: print boolean in plsql 
Sql :: return the number of records in a single table mysql 
Sql :: python get backup of sql 
Sql :: sql set data from a select query to a temp table and insert 
Sql :: sql join on comma separated field 
Sql :: double in sql server example 
Sql :: C# mysql data reader from two tables 
Sql :: sql count(*) 
Sql :: mysql count 
Sql :: sql update multiple tables 
Sql :: change sql global mode 
Sql :: select count concat string sql server 
Sql :: SQL division of an integer by another integer get float CAST 
Sql :: alter in sql 
Sql :: oracle disk group space 
Sql :: offset in postgresql example 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =