首页 > 解决方案 > MS ACCESS VBA 移动/复制文件

问题描述

我正在使用下面的代码来引用具有完整路径信息的表,以将文件从一个位置移动(或复制)到另一个位置。但是,它没有移动任何东西,而是根据我的 Debug.Print 消息完成(移动完成 2/22/2021 1:22:41 PM)。对我所缺少的有什么想法吗?

此外,我想构建文件位于源中的文件夹/子文件夹结构......但不知道如何实现......以及如何做到这一点的指针?

Sub copy_files_from_table()

Dim FSO As Object
Dim source As String
Dim destination As String
Dim SQL As String
Dim RS As DAO.Recordset
Dim db As DAO.Database

 SQL = "select * from file_test"
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set db = CurrentDb
 Set RS = db.OpenRecordset(SQL)

 source = RS!LocalFile
 destination = "C:\Temp\Test"


 While Not RS.EOF

If Dir(source, vbDirectory) <> "" Then 
    objFSO.CopyFolder source, destination:=destination  '
    
 Debug.Print "Move Folder Command Complete From: " & destination

Else

 End If
    
 RS.MoveNext

Wend

Debug.Print "Move Complete " & Now()
End Sub

感谢提供的任何帮助。

标签: vbams-access

解决方案


到目前为止,我已经获得了以下代码来处理文件路径 <259; 但是,任何更长的时间都会导致代码出错。因为我在编码方面很绿色:) 有什么建议可以绕过长文件路径名吗?

Sub CopyFilesFromTable2()

On Error GoTo ErrorHandler

Dim source As String
Dim destination As String
Dim FSO As New FileSystemObject
Dim SQL As String
Dim RS As DAO.Recordset
Dim db As DAO.Database



 'Test Table
  SQL = "select * from file_test"

 'Prod Table
 'SQL = "select * from file"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set db = CurrentDb
Set RS = db.OpenRecordset(SQL)

 source = RS!LocalFile
File = VBA.FileSystem.Dir(source)
destination = "D:\Temp\Test\"

With RS

If Not .BOF And Not .EOF Then

.MoveLast
.MoveFirst

While (Not .EOF)

FSO.CopyFile RS!LocalFile, destination

.MoveNext

Wend


 End If

.Close

End With


ExitSub:

Set RS = Nothing
'set to nothing
MsgBox "Done!"

Exit Sub

ErrorHandler:
MsgBox Err, vbCritical

Resume ExitSub


End Sub

推荐阅读