Retrieving the File Attributes/
This tips demonstrates how to use the GetFileAttributes API function to
retrieve the attributes of a file. The attributes include whether the file is
read-only or marked as a system or hidden file.
Module Code
Add the following code to a module:
Declare Function GetFileAttributes Lib
"kernel32" _
Alias "GetFileAttributesA" _
(ByVal lpFileName As String) As Long
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Form Code
Add a command button to a form with the
following code:
Private Sub Command1_Click()
Dim FileName As String, Attributes As Long
'Use the filename you want
FileName = "C:\MSDOS.SYS"
'Get the attributes with the GetFileAttributes API
Attributes& = GetFileAttributes(FileName)
'Check the returned value for the different
'attributes using the AND-Operator.
Print "FileName: " & FileName & ", Attributes: "
& Attributes&
Print
Print "hidden:" & vbTab & vbTab & _
CBool(Attributes& And FILE_ATTRIBUTE_HIDDEN)
Print "system:" & vbTab & vbTab & _
CBool(Attributes& And FILE_ATTRIBUTE_SYSTEM)
Print "archive:" & vbTab & vbTab & _
CBool(Attributes& And FILE_ATTRIBUTE_ARCHIVE)
Print "readonly:" & vbTab & _
CBool(Attributes& And FILE_ATTRIBUTE_READONLY)
End Sub
|