Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.
share|improve this question

4 Answers 4

up vote 4 down vote accepted

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%
share|improve this answer

Basically, you need to test for for the ProgramFiles(x86) environment variable to determine if you're in 64bit Windows or not. Here's a sample batch file.

if "%programfiles(x86)%zzz"=="zzz" goto 32BIT
echo 64-bit Windows installed
"%PROGRAMFILES(x86)%\MyApp\app.exe"
goto END

:32BIT
echo 32-bit Windows installed
"%PROGRAMFILES%\MyApp\app.exe"

:END
share|improve this answer

This is the best that I could come up with:

set strProgramFiles=%ProgramFiles%
if exist "%ProgramFiles(x86)%" set strProgramFiles=%ProgramFiles(x86)%
"%strProgramFiles%\MyApp\app.exe"
share|improve this answer

Another approach is to run the script under the 32-bit cmd.exe, so that it can take advantage of WOW64 filesystem redirection and environment variable modifications. If you can modify the caller, you don't even have to modify the script in question.

You could even have the script detect when it's executing under the 64-bit cmd.exe and restart itself using the 32-bit cmd.exe:

@echo off
if "%PROCESSOR_ARCHITECTURE%" == "x86" goto :x86
echo Restarting using Wow64 filesystem redirection: %0 %*
%SystemRoot%\SysWOW64\cmd.exe /c %0 %*
exit /b %ERRORLEVEL%

:x86
rem Rest of script follows...

Note that getting this hack wrong may result in spawning endless cmd.exe processes. That's why I used goto and a label instead of if ... ( ... ): command line parameters may contain parentheses, and if ... ( ... ) performs a greedy search for the first closing parenthesis.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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