Sign in to follow this  
Followers 0
sontal

Read Only Attributes Set Automatically

6 posts in this topic

Thank a priori for any assistance/help you can provide me with.

What I actually need to do is having a folder that when I save a file in it, the latter gets automatically read only attributes. I've done some research via Google, but I couldn't find anything that could work as a solution, till I remembered this spectacular program. Do you think that it's possible?

Thank again!

Share this post


Link to post
Share on other sites



How is the file saved? Also with the same script, which would make it very easy, or by another program?

Here's the whole scenario:

I scan some documents and I save them to a specific folder. I need those .pdf files to be saved with "read only" attribute enabled the moment I save them to that folder. Is this possible through AutoIT?

Share this post


Link to post
Share on other sites

You can check every x seconds and check the files in the directory to set Read Only values to the files. This script uses WMI to detect when a file is created or deleted as a trigger to check the files in the directory. Compile & run this script in the directory you wish to check, or change all instances of "@ScriptDir" to the directory you wish to check.

#include <Misc.au3>
#include <File.au3>
_Singleton(@ScriptName, 0)

Local $Directory = StringReplace(@ScriptDir, '\', '\\\\')
Local $StrComputer = "."
Local $ObjWMIService = ObjGet("winmgmts:\\" & $StrComputer & "\root\cimv2")
Local $ColMonitoredEvents = $ObjWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
         & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
         & "TargetInstance.GroupComponent= " _
         & "'Win32_Directory.Name=""" & $Directory & """'")

While 1
    Local $objEventObject = $ColMonitoredEvents.NextEvent()
    Select
        Case $objEventObject.Path_.Class() = "__InstanceCreationEvent"
            _CheckDirectory()
        Case $objEventObject.Path_.Class() = "__InstanceDeletionEvent"
            _CheckDirectory()
    EndSelect
    Sleep(10)
WEnd


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Functions Start;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func _CheckDirectory()
    Local $Array = _FileListToArray(@ScriptDir, '*', 1)
    If @error Then Return
    For $i = 1 To $Array[0]
        If _FileInUse(@ScriptDir & '\' & $Array[$i]) Then ContinueLoop  ;If File has open handle(s), skip
        If Not FileExists(@ScriptDir & '\' & $Array[$i]) Then ContinueLoop  ;If File does not exist, skip
        If StringInStr(FileGetAttrib(@ScriptDir & '\' & $Array[$i]), 'R') Then
            FileSetAttrib(@ScriptDir & '\' & $Array[$i], 'R')   ;If File is not marked Read-Only, mark it Read-Only
        EndIf
    Next
EndFunc   ;==>_CheckDirectory

;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Parameter(s):     $sFilename = File name
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;
;===============================================================================
Func _FileInUse($sFilename)
    Local $aRet, $hFile
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
            "str", $sFilename, _ ;lpFileName
            "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ
            "dword", 0, _ ;dwShareMode = DO NOT SHARE
            "dword", 0, _ ;lpSecurityAttributes = NULL
            "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING
            "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
            "hwnd", 0) ;hTemplateFile = NULL
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        SetError($aRet[0])
        Return 1
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return 0
    EndIf
EndFunc   ;==>_FileInUse

Share this post


Link to post
Share on other sites

Thank you very much...I will try your script and revert in case of failure! :x

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0