Results 1 to 7 of 7

Thread: FILE_ATTRIBUTE(s)

  1. #1

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Question FILE_ATTRIBUTE(s)

    Work out attributes

    I need to work out the attributes for a file.
    I use the GetFileAttributes API call.
    How do I work out which attributes are set,
    from the returned Long variable.
    I know I can use If's for every combination but there must be
    an easier (lazier) way.

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    You didn't say what specific attributes you are looking for, but this example has constants for all of them. You should be able to modify this example to return the desired results.
    VB Code:
    1. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    2. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    3. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    4. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    5.  
    6. Const MAX_PATH = 260
    7. Const MAXDWORD = &HFFFF
    8. Const INVALID_HANDLE_VALUE = -1
    9. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    10. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    11. Const FILE_ATTRIBUTE_HIDDEN = &H2
    12. Const FILE_ATTRIBUTE_NORMAL = &H80
    13. Const FILE_ATTRIBUTE_READONLY = &H1
    14. Const FILE_ATTRIBUTE_SYSTEM = &H4
    15. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    16.  
    17. Private Type FILETIME
    18.     dwLowDateTime As Long
    19.     dwHighDateTime As Long
    20. End Type
    21.  
    22. Private Type WIN32_FIND_DATA
    23.     dwFileAttributes As Long
    24.     ftCreationTime As FILETIME
    25.     ftLastAccessTime As FILETIME
    26.     ftLastWriteTime As FILETIME
    27.     nFileSizeHigh As Long
    28.     nFileSizeLow As Long
    29.     dwReserved0 As Long
    30.     dwReserved1 As Long
    31.     cFileName As String * MAX_PATH
    32.     cAlternate As String * 14
    33. End Type
    34. Function StripNulls(OriginalStr As String) As String
    35.     If (InStr(OriginalStr, Chr(0)) > 0) Then
    36.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    37.     End If
    38.     StripNulls = OriginalStr
    39. End Function
    40.  
    41. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
    42.     'KPD-Team 1999
    43.     'E-Mail: [email][email protected][/email]
    44.     'URL: [url]http://www.allapi.net/[/url]
    45.  
    46.     Dim FileName As String ' Walking filename variable...
    47.     Dim DirName As String ' SubDirectory Name
    48.     Dim dirNames() As String ' Buffer for directory name entries
    49.     Dim nDir As Integer ' Number of directories in this path
    50.     Dim i As Integer ' For-loop counter...
    51.     Dim hSearch As Long ' Search Handle
    52.     Dim WFD As WIN32_FIND_DATA
    53.     Dim Cont As Integer
    54.     If Right(path, 1) <> "\" Then path = path & "\"
    55.     ' Search for subdirectories.
    56.     nDir = 0
    57.     ReDim dirNames(nDir)
    58.     Cont = True
    59.     hSearch = FindFirstFile(path & "*", WFD)
    60.     If hSearch <> INVALID_HANDLE_VALUE Then
    61.         Do While Cont
    62.         DirName = StripNulls(WFD.cFileName)
    63.         ' Ignore the current and encompassing directories.
    64.         If (DirName <> ".") And (DirName <> "..") Then
    65.             ' Check for directory with bitwise comparison.
    66.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    67.                 dirNames(nDir) = DirName
    68.                 DirCount = DirCount + 1
    69.                 nDir = nDir + 1
    70.                 ReDim Preserve dirNames(nDir)
    71.             End If
    72.         End If
    73.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    74.         Loop
    75.         Cont = FindClose(hSearch)
    76.     End If
    77.     ' Walk through this directory and sum file sizes.
    78.     hSearch = FindFirstFile(path & SearchStr, WFD)
    79.     Cont = True
    80.     If hSearch <> INVALID_HANDLE_VALUE Then
    81.         While Cont
    82.             FileName = StripNulls(WFD.cFileName)
    83.             If (FileName <> ".") And (FileName <> "..") Then
    84.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    85.                 FileCount = FileCount + 1
    86.                 List1.AddItem path & FileName
    87.             End If
    88.             Cont = FindNextFile(hSearch, WFD) ' Get next file
    89.         Wend
    90.         Cont = FindClose(hSearch)
    91.     End If
    92.     ' If there are sub-directories...
    93.     If nDir > 0 Then
    94.         ' Recursively walk into them...
    95.         For i = 0 To nDir - 1
    96.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
    97.         Next i
    98.     End If
    99. End Function
    100. Sub Command1_Click()
    101.     Dim SearchPath As String, FindStr As String
    102.     Dim FileSize As Long
    103.     Dim NumFiles As Integer, NumDirs As Integer
    104.     Screen.MousePointer = vbHourglass
    105.     List1.Clear
    106.     SearchPath = Text1.Text
    107.     FindStr = Text2.Text
    108.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    109.     Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
    110.     Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    111.     Screen.MousePointer = vbDefault
    112. End Sub

  3. #3

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    These are the constants summed to make the return Long value.

    Public Const FILE_ATTRIBUTE_NORMAL = &H80
    Public Const FILE_ATTRIBUTE_READONLY = &H1
    Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Public Const FILE_ATTRIBUTE_COMPRESSED = &H800
    Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Public Const FILE_ATTRIBUTE_SYSTEM = &H4
    Public Const FILE_ATTRIBUTE_TEMPORARY = &H100


    Any ideas?

  4. #4

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Thanks Hack,

    I know how to get the values(s), but if more than one attribute is set how do I work out which are.
    I know I can compare every possible value, but i want a quicker,
    'nicer' way.

    Thanks

  5. #5

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Bit set test

    I think the only way to do this is to do a bit set compare.
    Anybody know how to convert &H numbers into an array
    of bits?
    Then the bit set compare would be straightforward.

  6. #6

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    I think I've sorted this myself.
    Using bit testing of binary representation of the Constant value
    and the API return value

    Thanks anyway.

  7. #7

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Talking RESOLVED - File_ATTRIBUTES

    IT WORKS

    Finally worked something out all by myself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width