Jump to content
qwert

Is there a proper way to use %ProgramFiles%?

Recommended Posts

qwert

I've just run upon this, and haven't found a definitive answer on the forums.

Can %ProgramFiles% be used in a statement?  Yes, @ProgramFilesDir does work, but %ProgramFile% is the common reference throughout msdn, for example.

To cite a specific example, this is a recommended way to open WordPad: %ProgramFiles%\Windows NT\Accessories\wordpad.exe

Thanks in advance for any help.

Share this post


Link to post
Share on other sites
JLogan3o13

AutoIt is not batch, nor vbscript, despite its similarities. Natively you are not going to be able to use %ProgramFiles%; the closest you could get would be with EnvGet (look in the help file). And if you're going to go that far, you're basically doing the same as the @ProgramFilesDir macro anyway.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
qwert

Thanks for the quick response.  I had seen the EnvGet() calls and thought them a bit complicated for such a simple use. 

I'll stick with the @ProgramFilesDir and try to mentally translate whenever I see the use of %ProgramFiles%.

Share this post


Link to post
Share on other sites
Subz

You have to remember that this won't always work especially on 64 bit systems, personally I use something like the following to return the correct file path:

_EnvReplace(RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WORDPAD.EXE", "")

Func _EnvReplace($_sEnvString = "")
        Local $aEnvVariables[5][2]
        $aEnvVariables[0][0] = 4
        $aEnvVariables[1][0] = "%CommonProgramFiles%"
        $aEnvVariables[1][1] = @OSArch = "x64" ? RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", "CommonW6432Dir") : @CommonFilesDir
        $aEnvVariables[2][0] = "%CommonProgramFiles(x86)%"
        $aEnvVariables[2][1] = @OSArch = "x64" ? RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", "CommonFilesDir (x86)") : @CommonFilesDir
        $aEnvVariables[3][0] = "%ProgramFiles%"
        $aEnvVariables[3][1] = @OSArch = "x64" ? RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramW6432Dir") : @ProgramFilesDir
        $aEnvVariables[4][0] = "%ProgramFiles(x86)%"
        $aEnvVariables[4][1] = @OSArch = "x64" ? RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir (x86)") : @ProgramFilesDir
        For $i = 1 To $aEnvVariables[0][0]
            $_sEnvString = StringReplace($_sEnvString, $aEnvVariables[$i][0], $aEnvVariables[$i][1])
        Next
        Return $_sEnvString
EndFunc

 

Share this post


Link to post
Share on other sites
Tekk

You CAN do this with "Opt" or "AutoItSetOption".

Opt("ExpandEnvStrings", 1)

MsgBox(0, "ExpandEnvStrings", "%ProgramFiles%\Windows NT\Accessories\wordpad.exe")

 

Share this post


Link to post
Share on other sites
Subz

@Tekk Unless of course the script is compiled as 32 bit on an 64-bit OS in which case it will return C:\Program Files (x86)\... rather than C:\Program Files\..., this wouldn't really matter in Wordpads case as they have 32-bit Wordpad and a 64-bit Wordpad but in most cases it would fail because it would return the wrong path.

Share this post


Link to post
Share on other sites
qwert
Posted (edited)
4 minutes ago, Subz said:

Unless of course the script is compiled as 32 bit on an 64-bit OS

I'll have to consider that case ... because that's exactly what I'm doing on my current Win7 PC.

Thanks for pointing that out.

Edited by qwert

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

×