LATEST UPDATES
ShareHolder Finder v0.9.3 is now released!!! Check Product Page for more details.
✉ info@automationdevelopers.com

ITS ALL ABOUT AUTOMATION

Friday, 15 April 2016

AutoIt - Check if a file path is a folder or a file


Author Post Date Friday, 15 April 2016
We can get the attributes of a file or folder using FileGetAttrib ("file/ folder path") function.

It returns a code string representing the file/ folder attributes. The code string can contain the combination of following letters:
    "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

In the case of error, it returns "" (empty string) and sets the @error flag to 1.

To check if a file path is a folder/ directory or a file, we need to check if the returned string contains the letter "D". If the path is a folder/ directory, the attribute string will contain the letter "D".

In the below example, we will use the current script file path and script directory to check the Directory attribute.

Example:

#include <MsgBoxConstants.au3>

;Assign a variable with the current script file path to check on whether it's a file or not.
Local $sFilePath = @ScriptFullPath

;Calls the IsFile() function which defines the attribute checking.
IsFile($sFilePath)

;Assign a variable with the current script directory to check on whether it's a directory/folder or not.
$sFilePath = @ScriptDir

;Calls the IsFile() function which defines the attribute checking.
IsFile($sFilePath)

;Check if the file path is a file or a directory/ folder.
Func IsFile($sFilePath)
    ;Get the attribute string and check if it contains the letter "D".
    If StringInStr(FileGetAttrib($sFilePath), "D") = 0 Then
        MsgBox($MB_SYSTEMMODAL, "", "The path" & " '" &  $sFilePath & "' " & "is a file.")
    Else
        MsgBox($MB_SYSTEMMODAL, "", "The path" & " '" &  $sFilePath & "' " & "is a directory.")
    EndIf
EndFunc   ;==>IsFile

Related Posts




Interesting? Share and Let Others Know.

Post a Comment