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?

Salman A
  • 262,204
  • 82
  • 430
  • 521
Arek
  • 427
  • 2
  • 6
  • 8

5 Answers5

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
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 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 03 '13 at 09: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 03 '13 at 10:04
  • Yep, use %ProgramFiles(x86)% or %ProgramFiles% in conditions and PPATH Check http://stackoverflow.com/questions/9594066/how-to-get-program-files-x86-env-variable – Sgali May 03 '13 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  
Olotiar
  • 3,225
  • 1
  • 18
  • 37
Prashant
  • 51
  • 1
  • 1
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.

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.

Daniel
  • 27,718
  • 20
  • 89
  • 133
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.

Salman A
  • 262,204
  • 82
  • 430
  • 521