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:
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
;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
- AutoIt - Set a file or folder as Read Only
- AutoIt - Set a file or folder as Read Write
- AutoIt - Hide a file or folder
- AutoIt - Unhide a file or folder
- AutoIt - Check if a file or folder is Read Only
- AutoIt - Check if a file or folder is Read Write
- AutoIt - Check if a file or folder is Hidden
Interesting? Share and Let Others Know.
Post a comment