Jump to content
trof

working dir and RequireAdmin

Recommended Posts

Hi,

I use this script to save the list of files present in a directory. I launch it from Directory background context menu.

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
Opt("ExpandEnvStrings", 1)
#include <File.au3>
Local $aFileList = _FileListToArrayRec(@WorkingDir & "\", "*", 0 +8, 1, 1, 1)
For $i = 1 To UBound($aFileList) - 1
    Local $sFilePath = $aFileList[$i]
    If Not FileWrite("elenco relative.txt", $sFilePath & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    EndIf
Next


It works well: output is saved on the working directory.


However It doesn't work on folder who requires high privilegies, so I have added

#RequireAdmin


Now the script doesn't work well anymore because the working dir is changed and it is C:\Windows\System32

Share this post


Link to post
Share on other sites

Why don't you just specify the working directory, or use a different macro? There are a list of others here: https://www.autoitscript.com/autoit3/docs/macros.htm

When you elevate to admin access your working directory always switches to System32, this can be seen if you start an elevated command prompt vs. a normal one. If you want the user directory you can use

Local $aFileList = _FileListToArrayRec(@UserProfileDir & "\", "*", 0 +8, 1, 1, 1)

 


UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

Hello. how do you add your script to the  Directory background context menu ?

I think is better to pass the current directory path as commanline parameter. so you can do something like this:

 

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\TestApp]
@="Test Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\TestApp\command]
@="\"C:\\Users\\MyUserName\\Desktop\\MyAppp.exe\" \"%V\""

Then you can handle this way

 

Local $sWorkingDir=$CmdLine[1]
Local $aFileList = _FileListToArrayRec($sWorkingDir & "\", "*", 0 +8, 1, 1, 1)

 

 

Saludos

Share this post


Link to post
Share on other sites

Sorry, I was offline for a while.

 

12 hours ago, anthonyjr2 said:

Why don't you just specify the working directory, or use a different macro? There are a list of others here: https://www.autoitscript.com/autoit3/docs/macros.htm

When you elevate to admin access your working directory always switches to System32, this can be seen if you start an elevated command prompt vs. a normal one. If you want the user directory you can use

Local $aFileList = _FileListToArrayRec(@UserProfileDir & "\", "*", 0 +8, 1, 1, 1)

 

I prefer to not specify the working directory, so I can use the script in every path.

 

10 hours ago, Danyfirex said:

Hello. how do you add your script to the  Directory background context menu ?

I think is better to pass the current directory path as commanline parameter. so you can do something like this:

 

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\TestApp]
@="Test Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\TestApp\command]
@="\"C:\\Users\\MyUserName\\Desktop\\MyAppp.exe\" \"%V\""

Then you can handle this way

 

Local $sWorkingDir=$CmdLine[1]
Local $aFileList = _FileListToArrayRec($sWorkingDir & "\", "*", 0 +8, 1, 1, 1)

 

 

Saludos

I attach my registry entries.

Your script works well. However, if I use it with my registry key I have this error

c9RfnJu.jpg

background.zip

Edited by trof
I made an error

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

×
×
  • Create New...