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 or folder is Hidden


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 the Hidden attribute of a file or folder, we need to check if the returned string contains the letter "H". If the file or folder is hidden, the letter "H" will be present.

In the below example, we will set a file to hidden and read-back and check the file attribute. Refer to AutoIt - Hide a file or folder for details on setting a file or folder as hidden.

Example:

#include <MsgBoxConstants.au3>

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

;Hide C:\AutomationDevelopers\temp.txt.
Local $bStatus = FileSetAttrib($sFilePath, "+H")

If $bStatus Then
    ;If the returned value is 1, display success.
    MsgBox($MB_SYSTEMMODAL, "SUCCESS", "File attribute set to hidden.")
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 "H" and display appropriate message.
If StringInStr($sAttribute,"H") Then
    MsgBox($MB_SYSTEMMODAL, "Info", "The file is hidden.")
Else
    MsgBox($MB_SYSTEMMODAL, "Info", "The file is not hidden.")
EndIf

Related Posts




Interesting? Share and Let Others Know.

Post a Comment