vba - 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
感谢提供的任何帮助。
解决方案
到目前为止,我已经获得了以下代码来处理文件路径 <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
推荐阅读
- c++ - 如果是一个类的私有数据成员并且我们创建该类的两个对象,那么指向 int 数据类型的指针是什么?
- python - 如何根据变量(例如日期)循环访问 URL?
- python-3.x - python中的嵌套if语句
- ios - XXX 不支持 Azure 构建管道中的配置文件
- powershell - 无法在 Pester 测试中输出文件
- swift - 从日期选择器 swift 中隐藏过去的日期和时间
- java - 在主窗口上单击菜单项时,主窗口的内容窗格内的 Java 已打开窗口位于主窗口下方
- python - 循环熊猫数据框
- nginx - Nginx 反向代理替换部分位置 URL
- javascript - 使 setState() 改变整个班级的状态变量