Windows – Correct technique to find application in 32 and 64 bit versions of Vista/Windows 7 from CMD.EXE

64-bitbatch-filewindowswindows-command-prompt

BACKGROUND

I have an existing CMD script that works fine. It launches an app from PROGRAM FILES like so

"%PROGRAMFILES%\MyApp\app.exe" 

PROBLEM

  • it works fine on 32-bit versions of Windows (Vista, Windows 7)
  • but on 64-bit versions of Windows the app will be installed into "Program Files (x86)" and not "Program Files" (which is what happens on the 32bit OS)

WHAT I AM LOOKING FOR

  • A script that robustly handles both cases (i.e. it "does the right thing" depending on the OS it is on)
  • a method that uses only those features found in CMD.EXE. I Am curious about solutions that use Powershell, etc, but those don't help me – Powershell will not be on the machines this script will run.

Best Answer

Similar to Matt's correct answer. Basically in this version the complete path is verified.

SET AppExePath="%ProgramFiles(x86)%\MyApp\app.exe"
IF NOT EXIST %AppExePath% SET AppExePath="%ProgramFiles%\MyApp\app.exe"
%AppExePath%