6

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.

6 Answers 6

5

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%
5

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"
2

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
0

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.

0

I would simply do this in one (very) robust line:

"%PROGRAMFILES%\MyApp\app.exe" || "%ProgramFiles(x86)%\MyApp\app.exe"

The || executes the second command only, if the previous command failed (aka "not found").

-2

THE SCRIPT BELOW CAN DISPLAY THE OS TYPE OF A WINDOWS PC

@echo off
echo Checking OS Architecture....
echo Architecture identified!
echo.
set OSA=%processor_architecture%
IF %OSA%==AMD64 (
ECHO This is a 64-bit Operating System
) ELSE (  
echo This is a 32-bit Operating System
)    
echo.
Pause
1
  • This is incomplete and may fail on some (rare - but the question was for a "robust" solution) machines. Possible values for %processor_architecture% are AMD64, IA64, ARM64, EM64T and X86 in which you script would fail in 3/5 cases.
    – bjoster
    Mar 31, 2021 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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