Jump to content

SetVar to the Root drive letter the exe is run from


Recommended Posts

Can I use EnvSet or anyother function to set a env var the drive letter that a given compiled exe is run from? From the looks in the help on EnvSet It doesnt work with relative paths.

Currently I am using a script that I made in conjunction with setx.exe and using FileInstall in my compiled setdrive.exe. But im sure this same thing can be done with autoit without the need of any outside programs or files.

Thanks,

P.S: Someday I will be proficient in AutoIt so I dont ask dumb questions. But until then you guys are all great and thanks for all the help you have given.

MrChris

Edited by MrChris
Link to comment
Share on other sites

Not sure of this can be done or not but I was thinking of have it be static to the rest of the OS so that I can use the VAR with other applications that might need to use absolute paths.

All this to help me in making some applications runs from my USB drive.

MrChris

Link to comment
Share on other sites

Well. My objective is to be able to have a single EXE or code that I can use in portable app loaders I make in AutoIt that when run from Any subfolder it will set a environment var equal to the drive letter that the exe is run from. So if I run F:\folder\subfolder\usb.exe my var would be %usbdrive% = F:\ or F: but at the same time have it be a var that can be used outside of Autoit by other windows applications/programs. I am using this var to help in making more and more apps portable so I can use them on my USB thumb drive.

I currently have compiled a exe that does this but has some limitations not to mention I am using setx.exe with it and the exe must be run from the root of the drive (not in a subfolder).

I was hoping to be able to run the EXE from any folder and be able to set the var as jsut the drive letter regardless of the folder in which it is run from. Plus I was hoping to be able to not use external app to set the var if AutoIt can do just this.

The below is working fine but has limitations.

usbdrive.au3

TraySetState (2)
; This script simply sets a envirment var based on the root of the drive in which the exe is 
; launched from.

; This will include setx.exe when comiling and EXE and extract it to the root path when the exe is run.
FileInstall("setx.exe", "")

; This will launch the setx.exe command to set the env var as %usbdrive%.
RunWait(@ComSpec & ' /c setx.exe usbdrive %cd%', '', @SW_HIDE)
Edited by MrChris
Link to comment
Share on other sites

This should help to set your Env.Variable (Switch is a latest Beta keyword)

_Variable('System', 'usbdrive', StringLeft(@ScriptDir, 1) & ':')

Func _Variable($type, $key, $value)
    ; adds a permanent environment variable.
    Local $environment
    Switch $type
        Case 'System'
            $environment = 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
        Case 'User'
            $environment = 'HKCU\Environment'
        Case Else
            Return
    EndSwitch
    If RegRead($environment, $key) Then
        If MsgBox(0x40021, @ScriptName, 'Overwrite Permanent Environmental Variable value: "' & $key & '", with data: "' & $value & '"') = 2 Then Return 0
    EndIf
    If RegWrite($environment, $key, 'Reg_Expand_sz', $value) Then EnvUpdate()
EndFunc

It would not be available until the next script is executed. But SetX needs a reboot to use the Env.Variable so are better with AutoIt to do it.

:D

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...