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).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback