We can set the attributes of a file or folder using FileSetAttrib ("Path of the file(s)/ folder(s)", "+-RASHNOT" [, recurse = 0]) function.
Path of the file(s)/ folder(s) - The path of the file(s) to set, e.g:- C:\*.txt, C:\AutomationDevelopers. (wildcards * and ? accepted).
To set a file or folder as read-write, we need to use "-R".
Path of the file(s)/ folder(s) - The path of the file(s) to set, e.g:- C:\*.txt, C:\AutomationDevelopers. (wildcards * and ? accepted).
+-RASHNOT - Attribute(s) to set/clear. + is used to set and - is used to clear. e.g:- "+R", "+AH-R".
The attributes that can be modified with the function are +/-:
"R" = READONLY
"A" = ARCHIVE
"S" = SYSTEM
"H" = HIDDEN
"N" = NORMAL
"O" = OFFLINE
"T" = TEMPORARY
"R" = READONLY
"A" = ARCHIVE
"S" = SYSTEM
"H" = HIDDEN
"N" = NORMAL
"O" = OFFLINE
"T" = TEMPORARY
recurse [optional] - $FT_NONRECURSIVE (0) - no recursion (Default)
$FT_RECURSIVE (1) - folders are recursed into.
Constants are defined in FileConstants.au3.
It returns 1 for success and 0 for failure.
In the below example, we will use the same file created with the example program in AutoIt - Write data to a file.
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
;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
- AutoIt - Set a file or folder as Read Only
- 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
- AutoIt - Check if a file path is a folder/ directory or a file
Interesting? Share and Let Others Know.
Post a comment