Library code snippets

File Attributes

This example demonstrates retrieving file attributes such as ReadOnly, Archive, System etc. Add a command button called cmdGet, and a textbox called txtFile. Add the code below, and run your project. Enter a filename into txtFile, and click the button to retrieve the properties.

Private Sub cmdGet_Click()
    GetFileInfo (txtfile)
End Sub

Sub GetFileInfo(sFile As String)
    Dim sFileAttrib As Long
    Dim sFileInfo As String
    sFileAttrib = GetAttr(sFile)
    ' Get Attibutes and fill attribute string
    If (sFileAttrib And vbReadOnly) = vbReadOnly Then
        sFileInfo = sFileInfo & "Read Only"
    End If
    If (sFileAttrib And vbArchive) = vbArchive Then
        sFileInfo = sFileInfo & " Archive"
    End If
    If (sFileAttrib And vbNormal) = vbNormal Then
        sFileInfo = sFileInfo & " Normal"
    End If
    If (sFileAttrib And vbSystem) = vbSystem Then
        sFileInfo = sFileInfo & " System"
    End If
    If (sFileAttrib And vbHidden) = vbHidden Then
        sFileInfo = sFileInfo & " Hidden"
    End If
    If (sFileAttrib And vbDirectory) = vbDirectory Then
        sFileInfo = sFileInfo & " Directory"
    End If
    MsgBox sFile & " has the following properties: " & sFileInfo
End Sub

Comments

  1. 01 Dec 2003 at 04:06

    Good code works for me  BUT I do get some strange results..   some files return an unknown attribute.  When doing a simply x = getattr(filename) some are over 8000???   Any ideas what attribute this is?
    OS=winXP
    s/w=VB6

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of File Attributes.

Leave a comment

Sign in or Join us (it's free).