vba,遍历文件夹,排除隐藏文件
时间: 2024-03-05 09:22:06 浏览: 232
可以通过设置文件属性来排除隐藏文件,以下是一个VBA示例代码:
```
Sub TraverseFolders()
Dim fso As Object
Dim folder As Object
Dim subfolder As Object
Dim file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\TestFolder")
For Each subfolder In folder.SubFolders
For Each file In subfolder.Files
If (file.Attributes And 2) = 0 Then 'check if file is not hidden
'do something
End If
Next
TraverseSubfolders subfolder
Next
Set file = Nothing
Set subfolder = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
```
该代码将遍历名为“TestFolder”的文件夹及其所有子文件夹,并排除隐藏文件。如果文件属性中包含数字“2”,则表示该文件被标记为隐藏。
阅读全文