Jump to content

Is there a proper way to use %ProgramFiles%?

Recommended Posts

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.

Link to post
Share on other sites
  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites

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%.

Link to post
Share on other sites
  • 2 weeks later...

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])
        Return $_sEnvString


Link to post
Share on other sites

@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.

Link to post
Share on other sites
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
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...