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 "RASHNDOCTXJ":
    "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
    "J" = JOIN FOLDER (created as by FileCreateNTFSLink())

Related

FileExists, FileGetSize, FileGetTime, FileSetAttrib, FileSetTime, FileCreateNTFSLink

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,J", ",")

    ; 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 /, JoinFolder /", ",")

    ; 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

Example 4

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create a temporary dir
    Local $sDirPath = @TempDir & "\TempDir"
    DirRemove($sDirPath)
    DirCreate($sDirPath)

    ; Create a NTFS link to the document dir
    Local $sNTFSLink = $sDirPath & "\ExampleNTFSLinkToFolder"
    Local $iNTFSLink = FileCreateNTFSLink(@MyDocumentsDir, $sNTFSLink)

    ; Display an explorer of whether the NTFS link was created.
    Local $hWin = 0
    If $iNTFSLink Then
        ShellExecute($sDirPath)
        $hWin = WinWaitActive("[CLASS:CabinetWClass]", "") ; Wait explorer window active
    Else
        MsgBox($MB_SYSTEMMODAL, "", "The NTFS link wasn't created." & @CRLF & "FileCreateNTFSLink returned: " & $iNTFSLink)
    EndIf

    If IsNTFSLink($sNTFSLink) Then
        MsgBox($MB_SYSTEMMODAL, "", "The dirpath is a NTFSLink to a folder." & @CRLF & @CRLF & _
                "You can click on it.")
    Else
        MsgBox($MB_SYSTEMMODAL, "", "The dirpath is not a NTFSLink to a folder.")
    EndIf

    ; Delete the temporary dirs.
    DirRemove($sNTFSLink)
    DirRemove($sDirPath)

    ; close explorer windows
    If $hWin Then WinKill($hWin, "")

EndFunc   ;==>Example

; Check if the dirpath is a NTFSLink to a folder..
Func IsNTFSLink($sDirPath)
    Return StringInStr(FileGetAttrib($sDirPath), "J") > 0
EndFunc   ;==>IsNTFSLink