从一个文件夹移动文件时出错

时间:2015-07-07 12:20:59

标签: sql-server sql-server-2012

我有一个查询将文件从一个文件夹( Source )移动到另一个文件夹(目标) 我已经同意网络服务和本地服务的源和目的地,我收到错误

  

系统找不到指定的文件

我已经测试了文件夹以查看我是否能够访问这些文件而且我能够,当我得到文件时它似乎正常。当我尝试将文件移动到Destination文件夹时,我收到此错误。

我的查询如下,我创建一个临时表链接到我的源文件夹并将其内容插入其中,然后我将其过滤到另一个临时表,然后我获取我想要移动的文件然后使用游标迭代它并移动文件。

我希望这可以提供帮助。

Create Table #tmpDir
                (
                ID int IDENTITY(1,1),
                fName varchar(400)
                )
         Declare @dir varchar(100)
         Declare @folderPath varchar(500)
         set @folderPath = '\\Server1\Documents\Ocs\Inbox\'
         set @dir = 'DIR ' + '"' + @folderPath + '"'
         print @dir
         INSERT #tmpDir EXECUTE master.dbo.xp_cmdshell @dir
         DELETE FROM #tmpDir where ID < 6
         SELECT SUBSTRING(FName,40,100) fileName2
                into #THIS
         FROM #tmpDir
         WHERE FName not like '%<DIR>%' 
         and FName not like '%bytes%' and FName is not null

Delete #THIS Where fileName2 Not Like '%_MyFiles%'


Declare @FileMove varchar(100)
Declare cur cursor for

  Select fileName2 From #THIS

Open cur

Fetch next from cur into @FileMove

WHILE @@FETCH_STATUS = 0
   BEGIN

Declare @cmd varchar(1000)
   set @cmd = 'Move /y "\\Server1\Documents\Ocs\Inbox\' + @FileMove + '     "\\Server1\ImportMac\Inbox"'
   EXECUTE master.dbo.xp_cmdshell @cmd

        Fetch next from cur into  @FileMove
                 END
Close cur
Deallocate cur

Drop Table #tmpDir, #THIS

1 个答案:

答案 0 :(得分:2)

您需要在move命令的源文件名末尾加上双引号:

set @cmd = 'Move /y "\\Server1\Documents\Ocs\Inbox\' + @FileMove + '"     "\\Server1\ImportMac\Inbox"'