下面是一个非常简单的代码,用于列出指定文件夹中的文件。它按预期工作。
但是,当我提供此文件夹的备份文件夹之一时,它会抛出无效路径(从 win 资源管理器窗口复制的备份文件夹路径)。
例如。:
Set objFolder = objFSO.GetFolder("D:\Test")** '效果很好
设置 objFolder = objFSO.GetFolder("D:\Test (2015.2.12 3:12 PM)") '不起作用。这是 Win7 创建的文件夹测试的先前/备份版本
有没有办法在 Excel 中使用 VBA 列出备份文件夹文件?
Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:\Test")
'Set objFolder = objFSO.GetFolder("D:\Test (2015.2.12 3:12PM)") throws error
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'print file name
Cells(i + 1, 1) = objFile.Name
'print file path
Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
End Sub
备份文件夹的外观:
当我复制路径并粘贴它时,一些?字符出现在括号中:
\localhost\C$\Users\IBALLA1\Downloads (?Friday,?April?17,?2015,??9:05 AM)
最佳答案
您不能在文件名或文件夹名称中使用冒号。 : 在您拥有文件夹名称的时间里,无法创建并且不能已经存在。我不知道这是否是您的问题中的错字。
关于excel - VBA Excel : list files in backup (prev. 版)文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856775/