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

    1.TabControl控件有点类似于.net中TableControl控件 2.常用属性 2.1 currentTab页面加载时,默认打开第几个tab,从0开始 2.2 shouMenuButton ...

  2. Windows 安装并配置 MySQL 5.6

    Windows 下安装 MySQL 有两种方式,一种是下载安装包,根据提示一路 next 安装,不需要什么配置,比较简单:另一种是下载压缩包,通过命令和配置来安装,也不难,个人感觉更简单.本篇就采用第 ...

  3. WPF 重写微调自带的样式,ListView、DataGrid、TreeView等所有控件的默认样式

    不知道各位在开发中有没有遇到这样的窘迫,开发一个UI,设计给出的效果图和自带的样式的区别很大,然后有的样式通过属性是修改不了的,比如TreeView的子项TreeViewItem,想完全透明背景色就做 ...

  4. Day 7 深copy和浅Copy

    dict.fromkeys的用法 1 2 3 4 5 6 7 8 9 10 11 #dict.fromkeys的用法 #例子1 dic = dict.fromkeys([1,2,3],[]) prin ...

  5. python numpy 数组中元素大于等于0的元素

    >>> import numpy as np >>> a = np.random.randint(-5, 5, (5, 5)) >>> a arr ...

  6. python学习笔记6-集合

    # 集合是无序且不可重复的元素的集合 a = set([1,3,1,3,3,2,2,5]) a # {1, 2, 3, 5} b = set(range(2,5)) b # {2, 3, 4} # 1 ...

  7. 比特、字节、K

    比特(bit) 比特,计算机专业术语,是信息量单位,由英文BIT音译而来.BIT为Binary digit(二进制数)位的缩写.二进制数的一位所包含的信息就是一比特,如二进制数0100就是4比特. 字 ...

  8. h5 端图片上传

    1.upload.js (function($) { $.extend($.fn, { images : new Array(), initImages:function (images) { $.e ...

  9. SQL查询排名第二名的信息

    今天看见同学去面试的面试题,查询出某个字段排名第二名的信息,自己就看看 如果是Oracle ,这不就是考察Oracle分页么,以Oracle的emp表为例,根据薪水排名,查询排名第二的员工信息: se ...

  10. android 判断service是否正在运行

    public static boolean isServiceExisted(Context context, String className) { ActivityManager activity ...