ShareHolder Finder v0.9.3 is now released!!! Check Product Page for more details.


Friday, 15 April 2016

AutoIt - Set a file or folder as Read Only

Author Post Date Friday, 15 April 2016
We can set the attributes of a file or folder using FileSetAttrib ("Path of the file(s)/ folder(s)", "+-RASHNOT" [, recurse = 0]) function.

To set a file or folder as read-only, 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
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.


#include <MsgBoxConstants.au3>

;Assign the file path to a variable.
Local $sFilePath = "C:\AutomationDevelopers\temp.txt"

;Set C:\AutomationDevelopers\temp.txt as read-only.
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-only.")
    ;If the returned value is 0, display error.
    MsgBox($MB_ICONERROR, "Error", "Problem setting attribute.")

Related Posts

Interesting? Share and Let Others Know.

Post a comment