excel - VBA Excel : list files in backup (prev. 版)文件夹

标签 excel vba windows-7-x64 special-folders

下面是一个非常简单的代码,用于列出指定文件夹中的文件。它按预期工作。
但是,当我提供此文件夹的备份文件夹之一时,它会抛出无效路径(从 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

备份文件夹的外观:

enter image description here
enter image description here
enter image description here

当我复制路径并粘贴它时,一些?字符出现在括号中:
\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/

相关文章:

excel - 大家好。谁能帮我解决这个电源查询问题

vba - PowerPoint VBA - 特殊粘贴(增强型图元文件)错误

python - %USERPROFILE% python 环境变量

windows - 无法启动托管网络

excel - Office 2016 -> 2013 "compile error, can' t 查找项目或库”

excel - 将水平线延伸到图表区域的边缘

vba - Excel 选择案例类型不匹配错误

excel - 在 vba msgbox 中不使用 goto 循环的方法

excel - 每次在 Excel 中使用验证列表时激活宏

visual-studio-2010 - CUDA Visual Studio 2010 Express 构建错误