5

I want to write .bat script which works under all flavours of Windows, no matter if 32 or 64 bit.

In this script I want to run some file.exe. That file is located in C:\Program Files\ under 32-bit systems or C:\Program FIles (x86)\ under x64 systems. I can write:

"%ProgramFiles(x86)%\file.exe" under 64bit systems or "%ProgramFiles%\file.exe" under 32bit systems but I want to make the script universal. Is there any way of determining that path universally?

5 Answers 5

6

You could just check for its existence & store the path;

@echo off & setLocal enabledelayedexpansion
if exist "C:\Program Files\app1.exe" (
 set PPATH="C:\Program Files\"
) else (
 set PPATH="C:\Program Files(x86)\"
)

start "" %PPATH%app1.exe
start "" %PPATH%app2.exe
start "" %PPATH%app3.exe
3
  • This solution is error-prone. What if the OS is in French or German? Foldername for program files is different there, at least on XP (dunno about Vista).
    – Sgali
    May 3, 2013 at 9:41
  • 1
    Fair point if your operating in a non english locale, you could replace the hard coded path with %PROGRAMFILES% & %PROGRAMFILES(x86)% respectively
    – Alex K.
    May 3, 2013 at 10:04
  • Yep, use %ProgramFiles(x86)% or %ProgramFiles% in conditions and PPATH Check stackoverflow.com/questions/9594066/…
    – Sgali
    May 3, 2013 at 11:23
5

VBS script:

 set wshShell = createObject("WScript.Shell")  
    ''-----------------------------------    
    ''Get 32-bit program files path  
    ''-----------------  
    OsType = wshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")    
    If OsType = "x86" then  
            '''wscript.echo "Windows 32bit system detected"  
            strProgramFiles = wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")  
    elseif OsType = "AMD64" then  
            '''wscript.echo "Windows 64bit system detected"  
            strProgramFiles = wshShell.ExpandEnvironmentStrings("%PROGRAMFILES(x86)%")  
    end if  
1

"whereis" is a linux command to do that, but a windows port is available (unxutils, I believe). You don't get the path without it.

1

I think instead of a "bat" file you should use a VBScript/JScript file. Either of these scripts can be executed by Windows Script interpreter (wscript.exe/cscript.exe). The scripting environment and the interpreters are available on all flavors of windows so there is nothing to worry about. You can find code samples for traversing directory structure, checking file presence etc using VBScript. You can use the FileSystemObject Object for most part.

1

For simplicity's sake, you could always do the following:

cd %PROGRAMFILES%
cd %PROGRAMFILES(x86)%
Program.exe

This assumes you don't have to do anything complicated, but if setting the current directory is sufficient, this should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.