[转]COPY OR MOVE FILES AND FOLDERS USING OLE AUTOMATION

本文转自:http://sqlindia.com/copy-move-files-folders-using-ole-automation-sql-server/

 

I love playing around with automation stuff. In a recent automation task, I was to copy or move files and/or folders from one location to another in SQL Server without using SQLCMD.
If you are novice to OLE automation in SQL server then I would recommend you to read my previous articles on OLE automation first.

--The below configuration must be enabled 

/* 
sp_configure 'show advanced options', 1; 
GO 
RECONFIGURE with override; 
GO 

sp_configure 'Ole Automation Procedures', 1; 
GO 
RECONFIGURE with override; 
GO 
*/ 


DECLARE @source varchar(100) = 'E:\others\Lab_SQLIndia\src\TextFile.txt' 
, @destination varchar(100) = 'E:\others\Lab_SQLIndia\dest' 
, @operation tinyint = 1 -- 1=CopyFile, 2=CopyFolder, 3=MoveFile, 4=MoveFolder,   


DECLARE @fso int 
, @init int 
, @errFso int 
, @return varchar(100) 
SET @destination = @destination + '\' 

EXEC @init = sp_OACreate 'Scripting.FileSystemObject', @fso OUTPUT 
IF @init = 0 SELECT @errFso = @fso 

IF @init = 0 AND @operation = 1 


BEGIN 

IF @init = 0 SELECT @errFso = @fso 

EXEC @init = sp_OAMethod    @fso, 
                'CopyFile', 
NULL, @source, @destination SET @return = 'FILE SUCCESSFULLY COPIED' END ELSE IF @init = 0 AND @operation = 2 BEGIN IF @init = 0 SELECT @errFso = @fso EXEC @init = sp_OAMethod @fso, 'CopyFolder', NULL, @source, @destination SET @return = 'FOLDER SUCCESSFULLY COPIED' END ELSE IF @init = 0 AND @operation = 3 BEGIN IF @init = 0 SELECT @errFso = @fso EXEC @init = sp_OAMethod @fso, 'MoveFile', NULL, @source, @destination SET @return = 'FILE SUCCESSFULLY MOVED' END ELSE IF @init = 0 AND @operation = 4 BEGIN IF @init = 0 SELECT @errFso = @fso EXEC @init = sp_OAMethod @fso, 'MoveFolder', NULL, @source, @destination SET @return = 'FOLDER SUCCESSFULLY MOVED' END IF @init <> 0 BEGIN DECLARE @Description varchar (255), @src varchar (255), @Helpfile varchar (255), @HelpID int EXECUTE sp_OAGetErrorInfo @errFso, @src OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT PRINT COALESCE(@src + @Description + @Helpfile + CAST(@HelpID as varchar(20)), 'Error Occurred') END ELSE PRINT @return EXEC sp_OADestroy @fso

 

转载于:https://www.cnblogs.com/freeliver54/p/8063885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值