2

What would a safe way of extracting the 'Program Files' directory location using VBS?. I would like to get the directory location from the environment as it will avoid localization issues as well as problems between different OS architectures (32/64 bit) and drive (C:\, D:\, etc:).

So far I came across an example given on MSDN yet I can get the script to work in VBS, every run just complains about different errors. Here is what they've got on an example script for .net to get the Sys32 folder.

' Sample for the Environment.GetFolderPath method
Imports System

Class Sample
   Public Shared Sub Main()
      Console.WriteLine()
      Console.WriteLine("GetFolderPath: {0}", Environment.GetFolderPath(Environment.SpecialFolder.System))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'GetFolderPath: C:\WINNT\System32
'

As Helen mentioned, this is my script to determine the OS Architecture and depending on the outcome I wish to retrieve the respective 'Program Files' path

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & sPC  & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    sSystemArchitecture = objOperatingSystem.OSArchitecture
Next
Carlos
  • 5,405
  • 21
  • 68
  • 114
  • how are you running this? It looks more like VB.NET than VBS or VBA. –  Sep 26 '11 at 13:49
  • 1
    On 64-bit Windows, there're `Program Files` and `Program Files (x86)`. Which one do you need? – Helen Sep 26 '11 at 13:53
  • .. good point Helen. My code below returns x86 – brettdj Sep 26 '11 at 14:01
  • @oracle; true that. Sorry I mistakenly identified it as vbs. I meant something like that in vbs. @Helen; you are completely right, I am first checking whether the OS Architecture is 32 or 64 and depending I want to locate the respective folder. See update above – Carlos Sep 26 '11 at 14:02

1 Answers1

2

for vbs from How to get program files environment setting from VBScript

Set wshShell = CreateObject("WScript.Shell")
WScript.Echo wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")

if you are in vba

Sub GetMe()
Set wshShell = CreateObject("WScript.Shell")
MsgBox wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")
End Sub
Community
  • 1
  • 1
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • 1
    Thats excellent, thanks! The code above will return the 64bit 'Program Files' folder. The return 32 bit 'Program Files' folder under a x64 environment use `%PROGRAMFILES(x86)%` – Carlos Sep 26 '11 at 14:14
  • 4
    In VBA you could use `Environ("PROGRAMFILES")` – Chris Kent Sep 26 '11 at 14:22