获取隐藏文件的属性

时间:2015-04-21 09:52:44

标签: vba attributes hidden

我试图从指定的目录中获取所有[非空]文件属性。如何遍历目录并将文件设置为unhidden [" For Each ..."省略了他们]?

Sub SomeProc()

Dim sFile As Variant
Dim oShell: Set oShell = CreateObject("Shell.Application")
Dim oDir:   Set oDir = oShell.Namespace("c:\SomePath")

SetAttr "c:\SomePath\SomeFile.accdb", vbNormal

For Each sFile In oDir.Items
    For i = 0 To 288
        If oDir.GetDetailsOf(sFile, i) <> "" Then
            Debug.Print i & ": " & oDir.GetDetailsOf(oDir.Items, i) & ": " & oDir.GetDetailsOf(sFile, i)
        End If
    Next i
Next

SetAttr "c:\SomePath\SomeFile.accdb", vbHidden

End Sub

编辑:

感谢Alex K的提示[他的代码没有显示属性的值]我设法做到了:它可以工作,但我猜有性能问题[并且它没有&#39 ; t考虑子文件夹 - 不是问题]

Sub SomeProc()

Const PATH As String = "c:\SomePath\"

Dim sFile As Variant
Dim sFileName As String
Dim oShell As Object
Dim oDir As Object
Dim i, k As Long

Set oShell = CreateObject("Shell.Application")
Set oDir = oShell.Namespace(PATH)

sFileName = Dir$(PATH & "*.*", vbHidden)
k = 0
Do Until Len(sFileName) = 0
k = k + 1

Set sFile = oDir.parsename(sFileName)
For i = 0 To 288
    If oDir.GetDetailsOf(sFile, i) <> "" Then
        Sheets(1).Cells(i + 1, k).Value = i & ": " & oDir.GetDetailsOf(oDir.Items, i) & ": " & oDir.GetDetailsOf(sFile, i)
    End If
Next i

sFileName = Dir$()
Loop

End Sub

我也发现了不同的方法:http://www.techbookreport.com/tutorials/vb_fileattributes.html

1 个答案:

答案 0 :(得分:1)

GetDetailsOf()适用于隐藏文件,它只适用于没有隐藏文件的枚举。

使用内置的Dir$()功能列出文件,告诉它包含隐藏属性的文件

Sub SomeProc()
Const PATH As String = "c:\SomePath\"

Dim oShell: Set oShell = CreateObject("Shell.Application")
Dim oDir As Object
Dim sFileName As String
Dim i As Long

Set oDir = oShell.Namespace(PATH)
sFileName = Dir$(PATH & "*.*", vbHidden)

Do Until Len(sFileName) = 0
    For i = 0 To 288
        If oDir.GetDetailsOf(sFileName, i) <> "" Then
            Debug.Print i & ": " & oDir.GetDetailsOf(sFileName, i) & ": " & oDir.GetDetailsOf(sFileName, i)
        End If
    Next i

    sFileName = Dir$()
Loop
End Sub