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 the read write attribute of a file or folder, we need to check if the returned string contains the letter "R". If the file is read write, the letter "R" will not be present.
In the below example, we will set a file to read write and read-back and check the file attribute. Refer to AutoIt - Set a file or folder as Read Write for details on setting a file or folder as read write.
Example:
Example:
#include <MsgBoxConstants.au3>
;Assign the file path to a variable.
Local $sFilePath = "C:\AutomationDevelopers\temp.txt"
;Set C:\AutomationDevelopers\temp.txt as read-write.
Local $bStatus = FileSetAttrib($sFilePath, "-R")
If $bStatus Then
;If the returned value is 1, display success.
MsgBox($MB_SYSTEMMODAL, "SUCCESS", "File attribute set to read-write.")
Else
;If the returned value is 0, display error.
MsgBox($MB_ICONERROR, "Error", "Problem setting attribute.")
EndIf
;Get the file attributes and store it in a variable.
Local $sAttribute = FileGetAttrib($sFilePath)
;If error, display a message and exit.
If @error Then
MsgBox($MB_ICONERROR,"", "Error while getting the file attributes")
Exit
EndIf
;Check if the returned attribute string contains the letter "R" and display appropriate message box.
If StringInStr($sAttribute,"R") Then
MsgBox($MB_SYSTEMMODAL, "Info", "The file is read-only.")
Else
MsgBox($MB_SYSTEMMODAL, "Info", "The file is read-write.")
EndIf
;Assign the file path to a variable.
Local $sFilePath = "C:\AutomationDevelopers\temp.txt"
;Set C:\AutomationDevelopers\temp.txt as read-write.
Local $bStatus = FileSetAttrib($sFilePath, "-R")
If $bStatus Then
;If the returned value is 1, display success.
MsgBox($MB_SYSTEMMODAL, "SUCCESS", "File attribute set to read-write.")
Else
;If the returned value is 0, display error.
MsgBox($MB_ICONERROR, "Error", "Problem setting attribute.")
EndIf
;Get the file attributes and store it in a variable.
Local $sAttribute = FileGetAttrib($sFilePath)
;If error, display a message and exit.
If @error Then
MsgBox($MB_ICONERROR,"", "Error while getting the file attributes")
Exit
EndIf
;Check if the returned attribute string contains the letter "R" and display appropriate message box.
If StringInStr($sAttribute,"R") Then
MsgBox($MB_SYSTEMMODAL, "Info", "The file is read-only.")
Else
MsgBox($MB_SYSTEMMODAL, "Info", "The file is read-write.")
EndIf
- 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 Hidden
- AutoIt - Check if a file path is a folder/ directory or a file
Interesting? Share and Let Others Know.
Post a Comment