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.
user2666
  • 285
  • 2
  • 6
  • 8

6 Answers6

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"
Matt Hanson
  • 1,682
  • 1
  • 23
  • 34
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%
splattne
  • 28,508
  • 20
  • 98
  • 148
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
Joe Doyle
  • 1,681
  • 14
  • 15
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").

bjoster
  • 4,805
  • 5
  • 25
  • 33
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.

bk1e
  • 241
  • 2
  • 5
-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
yagmoth555
  • 16,758
  • 4
  • 29
  • 50
  • 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 '21 at 11:07