本文转自: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

[转]COPY OR MOVE FILES AND FOLDERS USING OLE AUTOMATION的更多相关文章

  1. Track files and folders manipulation in Windows

    The scenario is about Business Secret and our client do worry about data leakage. They want to know ...

  2. Mac OS finder : 显示和隐藏文件[夹] show and hide files or folders

    Finder默认是不显示隐藏文件[夹]的,要显示出怎么办? 要显示的话,可以GUI(graphic user interface)和CLI(command line interface)两种方式 CL ...

  3. depth: working copy\infinity\immediates\files\empty

    depth: working copy\infinity\immediates\files\empty 有时间,需要整理下,svn 合并深度这四项:具体的意思.

  4. [Bash] Move and Copy Files and Folders with Bash

    In this lesson we’ll learn how to move and rename files (mv) and copy (cp) them. Move index.html to ...

  5. Linux / mysql: is it safe to copy mysql db files with cp command from one db to another?

    Copying is very simple for MyISAM and completely 100% risky (near suicidal) with InnoDB. From your q ...

  6. How to copy remote computer files quickly to local computer

    if we want copy file from VM(Remote VM) to local computer. Always can not easy copy file so easy. no ...

  7. [Bash] Find Files and Folders with `find` in Bash

    find is a powerful tool that can not only find files but it can run a command on each matching file ...

  8. [Bash] View Files and Folders in Bash

    Sometimes when working at the command line, it can be handy to view a file’s contents right in the t ...

  9. C# copy source directory files with original folder to the destination path

    private static void PathCopyFilesWithOriginalFolder() { ; try { string sourceDir = @"E:\Source& ...

随机推荐

  1. monoDB环境搭建

    最近看到有部分人MongoDB安装之后总是启动不起来,在这里,写了一个简单的搭建教程 直接进入正题 1.mongoDB下载地址 https://www.mongodb.org/downloads#pr ...

  2. .net core api Post请求

    POST请求: 1.请求类型:Content-Type:application/json,参数:json字符串 后台接收参数: 1)([FromBody]UserInfo user)必须以对象形式接收 ...

  3. day 60 Django第一天

    jinjia2 : Jinja2是基于python的模板引擎,功能比较类似于于PHP的smarty,J2ee的Freemarker和velocity. 它能完全支持unicode,并具有集成的沙箱执行 ...

  4. day52 进程与守护进程

    http://www.cnblogs.com/Eva-J/articles/8253549.html 博客参考. 多进程聊天 守护进程. 多进程 1.Unix/Linux:fork()调用实现多进程. ...

  5. Swift 如何像 C语言 那样接收入口参数?

    我们都知道在 Swift 语言当中不再有 main 函数了,可能了解过 C语言或者 Java 语言的同学对这一点赶到深深的不适.总之,取而代之的是 main.swift. int main(int a ...

  6. 《Python黑帽子:黑客与渗透测试编程之道》 Web攻击

    Web的套接字函数库:urllib2 一开始以urllib2.py命名脚本,在Sublime Text中运行会出错,纠错后发现是重名了,改过来就好: #!/usr/bin/python #coding ...

  7. js代码上的优化

    例1  if ( config.url === '/web/teacher/classes'  || config.url === '/web/teacher/students || config.u ...

  8. jvm(2)类的初始化(一)

    [深入Java虚拟机]之三:类初始化 类初始化是类加载过程的最后一个阶段,到初始化阶段,才真正开始执行类中的Java程序代码. 1,下面说的初始化主要是类变量的初始化,实例变量的初始化触发条件不同(一 ...

  9. windows系统上安装与使用Android NDK r5

    windows系统上安装与使用Android NDK r5  很早就听说了android的NDK应用,只是一直没有时间去研究,今天花了点时间在windows平台搭建了NDK环境,并成功运行了第一个简单 ...

  10. iOS-UIScrollView内容复用【实现两个试图的复用】

    前言 这里说的内容复用,是指添加到 ScrollView 里面的试图是同一个模型:比如,我需要在 ScrollView 上添加100个 xkView(其他封装好的VC.UIView),每次滑动 Scr ...