Function Reference


FileGetAttrib

Returns a code string representing a file's attributes.

FileGetAttrib ( "filename" )

Parameters

filename The path to the file or directory to check.

Return Value

Success: a code string representing a file's attributes.
Failure: "" (empty string) and sets the @error flag to 1.

Remarks

String() returned could contain a combination of these letters "RASHNDOCTX":
    "R" = READONLY
    "A" = ARCHIVE
    "S" = SYSTEM
    "H" = HIDDEN
    "N" = NORMAL
    "D" = DIRECTORY
    "O" = OFFLINE
    "C" = COMPRESSED (NTFS compression, not ZIP compression)
    "T" = TEMPORARY
    "X" = EFS ENCRYPTION

Related

FileExists, FileGetSize, FileGetTime, FileSetAttrib, FileSetTime

Example

Example 1

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope to store the file attributes of the current script.
    Local Const $sAttribute = FileGetAttrib(@ScriptFullPath)

    ; Display the string returned by FileGetAttrib.
    MsgBox($MB_SYSTEMMODAL, "", "The attribute string: " & @CRLF & $sAttribute)

    ; Display the string returned by AttributeToString
    MsgBox($MB_SYSTEMMODAL, "", "The attribute string with easier to understand values: " & @CRLF & _
            AttributeToString($sAttribute))
EndFunc   ;==>Example

Func AttributeToString($sAttribute)
    ; Create a 1d array of the file attribute letters by splitting the string at the comma (,).
    Local $aInput = StringSplit("R,A,S,H,N,D,O,C,T", ",")

    ; Create a 1d array using the friendlier file attribute names by splitting the string at the comma (,).
    Local $aOutput = StringSplit("Read-only /, Archive /, System /, Hidden /" & _
            ", Normal /, Directory /, Offline /, Compressed /, Temporary /", ",")

    ; Loop through the attribute letters array to replace with the friendlier value e.g. A becomes Archive or S becomes System.
    For $i = 1 To $aInput[0]
        $sAttribute = StringReplace($sAttribute, $aInput[$i], $aOutput[$i], 0, $STR_CASESENSE)
    Next

    ; Remove the single space and trailing forward slash.
    $sAttribute = StringTrimRight($sAttribute, 2)

    ; Return the attribute string.
    Return $sAttribute
EndFunc   ;==>AttributeToString

Example 2

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Assign a variable with the filepath to check on whether it's a file or not.
    Local $sFilePath = @ScriptFullPath

    If IsFile($sFilePath) Then
        MsgBox($MB_SYSTEMMODAL, "", "The filepath is a file.")
    Else
        MsgBox($MB_SYSTEMMODAL, "", "The filepath is not a file.")
    EndIf
EndFunc   ;==>Example

; Check if the filepath is a file. Does not validate if the file exists.
Func IsFile($sFilePath)
    Return StringInStr(FileGetAttrib($sFilePath), "D") = 0
EndFunc   ;==>IsFile

Example 3

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Assign a variable with the filepath to check on whether it's a directory/folder or not.
    Local $sFilePath = @ScriptDir

    If IsDir($sFilePath) Then
        MsgBox($MB_SYSTEMMODAL, "", "The filepath is a directory/folder.")
    Else
        MsgBox($MB_SYSTEMMODAL, "", "The filepath is not a directory/folder.")
    EndIf
EndFunc   ;==>Example

; Check if the filepath is a directory/folder. Does not validate if the directory/folder exists.
Func IsDir($sFilePath)
    Return StringInStr(FileGetAttrib($sFilePath), "D") > 0
EndFunc   ;==>IsDir