0

How should I recode a Windows XP cmd batch file containing the commands below to work also on later Windows where the two .exes may be in Program Files (x86)?

start "GW" /WAIT "%PROGRAMFILES%\GoldWave\GoldWave.exe" [...arguments...]
start "BC" /WAIT "%PROGRAMFILES%\Beyond Compare 3\BCompare.exe" [...arguments...]

This fails:

PATH=%PATH%;%PROGRAMFILES%;%PROGRAMFILES(X86)%
...
start "GW" /WAIT "GoldWave\GoldWave.exe" [...arguments...]
start "BC" /WAIT "Beyond Compare 3\BCompare.exe" [...arguments...]

This succeeds but is clumsy:

PATH=%PATH%;%PROGRAMFILES%\Goldwave;%PROGRAMFILES(X86)%\Goldwave;%PROGRAMFILES%\Beyond Compare 3;%PROGRAMFILES(X86)%\Beyond Compare 3%
...
start "GW" /WAIT "GoldWave.exe" [...arguments...]
start "BC" /WAIT "BCompare.exe" [...arguments...]
ChrisJJ
  • 2,191
  • 1
  • 25
  • 38
  • 1
    [This question](http://stackoverflow.com/questions/601089/detect-whether-current-windows-version-is-32-bit-or-64-bit) could be of help. – Steve Jan 15 '13 at 23:23
  • " The default on English-language systems is C:\Program Files. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit " --- so for the solution below I should use %ProgramW6432% -- thanks! – ChrisJJ Jan 16 '13 at 23:35

1 Answers1

3

You can first check to see if %ProgramFiles(x86)% has a value. If it does, the programs you are looking for should be found in that directory:

SETLOCAL
SET PROGRAMFILESINUSE=%ProgramFiles%
IF NOT "%ProgramFiles(x86)%"=="" SET PROGRAMFILESINUSE=%ProgramFiles(x86)%
START "GW" /WAIT "%PROGRAMFILESINUSE%\GoldWave\GoldWave.exe"

Or, you could make it a little more involved and have it actually check where the file you are looking for exists:

SETLOCAL
SET PROGRAMDIRECTORY=
SET RELATIVEFILEPATH=GoldWave\Goldwave.exe
CALL :CHECKFORPROGRAM "%RELATIVEFILEPATH%" "%ProgramFiles(x86)%"
CALL :CHECKFORPROGRAM "%RELATIVEFILEPATH%" "%ProgramFiles%"
IF "%PROGRAMDIRECTORY%"=="" GOTO :NOTFOUND
ECHO Found at %PROGRAMDIRECTORY%\%RELATIVEFILEPATH%
GOTO :EOF
:CHECKFORPROGRAM
IF NOT "%PROGRAMDIRECTORY%"=="" GOTO :EOF
IF EXIST "%~2\%~1" SET PROGRAMDIRECTORY=%~2
GOTO :EOF
:NOTFOUND
ECHO %RELATIVEFILEPATH% not found
GOTO :EOF
Jon
  • 428,835
  • 81
  • 738
  • 806