Jump to content
Sign in to follow this  

Determine from an elevated script if current folder is (system) write protected

Recommended Posts


Hello :oops:

I hope I won't be redundant, I didn't find any answer with my several searches with search engines and the forum.

The context is the following:

I need to know if the current folder from where my script is running is a system protected folder or not, in order to determine where to write the .ini file which will keep settings of this script.

This script is running elevated so I can't just test to write a file from it as it would always work in any folder.

In case it is in a protected folder, I would write the .ini file in the current user's folder, like Microsoft says. In case it isn't, as my script is portable, I would write it next to the script itself.

Here is the workaround I found, I hope it can help or give ideas to others.

It only tests if given folder is protected or not.

; #FUNCTION# ====================================================================================================================
; Name ..........: _IsProtectedFolder
; Description ...: Test with a script running elevated if a folder is (system) protected.
;                   It tries to create a file in user mode via task scheduler in the folder defined, if created it is not protected else it is.
; Syntax ........: _IsProtectedFolder($Folder)
; Parameters ....: $folder                  -> The folder to test
; Return values .:                                  False: not protected folder
;                                                   True: protected folder
;                                                   Sets @error to 1 If folder doesn't exist
; Author ........: AvvA
; Remarks .......: Must be run from an elevated script.
;                   Windows Vista and Seven OSes.
;                   The '/SC ONSTART' argument of the created task is irrelevant, it only permits to not define other parameters like start/end hour.
;                   The '/RL LIMITED' is facultative as by default it should create a limited rights task
; Link ..........: http://www.autoitscript.com/forum/topic/138227-determine-from-an-elevated-script-if-current-folder-is-system-write-protected/
; Example .......: Yes
; ===============================================================================================================================
Func _IsProtectedFolder($Folder)
    ;mandatory tests
    ;you can remove this following test
    If Not IsAdmin() Then ;you must run this script elevated
        MsgBox(0, "Error", "You must run this function with administrative privileges." & @LF&@LF & "Add this line to the top of your script:" & @LF & "#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator" & @LF&@LF & "Or run SciTE elevated.")
    ;you shouldn't remove the 2 following tests
    If StringRight($Folder, 1) == '' Then $Folder = StringTrimRight($Folder, 1) ;remove an eventual anti-slash at the end of folder name
    If Not FileExists($Folder) Then ;the folder to test should exists ^^

    ;generate random names for the task and the test file
    Local $TestFileName = $Folder & '' & Random(100000000, 999999999, 1) & '-' & Random(100000000, 999999999, 1) & '-testingprotectedfolderfile.txt'
    Local $TaskName = Random(100000000, 999999999, 1) & '-randomtaskname'

    ;create a scheduled task which will try a file write in the folder defined
    ShellExecuteWait('schtasks.exe', '/Create /TN ' & $TaskName & ' /RL LIMITED /F /SC ONSTART /NP /TR "cmd /C ''type /? > ''' & $TestFileName & '''''"', '', '', @SW_HIDE)
    ShellExecuteWait('schtasks.exe', '/run /TN ' & $TaskName, '', '', @SW_HIDE)     ;launch this task
    ;Sleep(500) ;leave time for the file to be written, ie: if you comment the line below, you'll always get 'protected folder' answer
    ShellExecuteWait('schtasks.exe', '/delete /TN ' & $TaskName & ' /F', '', '', @SW_HIDE)  ;delete this task

    If FileExists($TestFileName) Then   ;test if file was written
        FileDelete($TestFileName)       ;delete this test file
        Return False    ;folder isn't protected
        Return True     ;folder is protected

;$Folder = @SystemDir & 'UsersPublic'   ;this folder most likely doesn't exist
;$Folder = @HomeDrive & 'UsersPublic'   ;it is not a protected folder
;$Folder = EnvGet("systemdrive")        ;usually C: -> it is a protected folder
;$Folder = EnvGet("windir")     ;usually C:Windows -> it is a protected folder
$Folder = @ScriptDir

$Protected = _IsProtectedFolder($Folder)

If @error Then
    $Result = 'not existing'
ElseIf $Protected Then
    $Result = 'protected'
    $Result = 'not protected'

MsgBox(0, "Test protected folder", $Folder & ' folder is ' & $Result)
Edited by AvvA

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