17

Does anybody know how to create a batch file that can shell one program if its a 64-bit system or shell another if its a 32-bit system?

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
philip
  • 345
  • 4
  • 5
  • 9
  • there are some answers here: http://stackoverflow.com/questions/601089/detect-whether-current-windows-version-is-32-bit-or-64-bit – Simon Mourier Feb 14 '11 at 10:07

9 Answers9

23

Check for %PROCESSOR_ARCHITECTURE% being x86:

if %PROCESSOR_ARCHITECTURE%==x86 (
  rem 32 bit
) else (
  rem 64 bit
)

At least for the time being. On a server I have access to it's AMD64 but no clue how Itanium looks like, for example. But 32-bit versions always report x86.

Another option, that also works on WoW64:

for /f "skip=1 delims=" %%x in ('wmic cpu get addresswidth') do if not defined AddressWidth set AddressWidth=%%x

if %AddressWidth%==64 (
  rem 64 bit
) else (
  rem 32 bit
)
Joey
  • 344,408
  • 85
  • 689
  • 683
  • @Mehrdad: fixed. Although I wonder who uses a dedicated 32-bit `cmd` instance to do work on a 64-bit system. – Joey Feb 16 '11 at 10:03
  • 1
    Interesting solution! :) Well, it's actually not necessarily because the user has a choice; rather, the `cmd` instance might be already started by a 32-bit process, and the user might only be supplying commands, with no control over what started the command prompt. (Oh, by the way: that solution won't work if WMI is, for any reason, disabled.) – user541686 Feb 16 '11 at 17:07
13

It works without WMI too! I suggest:

 @echo off
 if /i "%processor_architecture%"=="AMD64" GOTO AMD64
 if /i "%PROCESSOR_ARCHITEW6432%"=="AMD64" GOTO AMD64
 if /i "%processor_architecture%"=="x86" GOTO x86
 GOTO ERR
 :AMD64
    rem do amd64 stuff
 GOTO EXEC
 :x86
    rem do x86 stuff
 GOTO EXEC
 :EXEC
    rem do arch independent stuff
 GOTO END
 :ERR
 @echo Unsupported architecture "%processor_architecture%"!
 pause
 :END
Josef
  • 1,467
  • 2
  • 24
  • 40
  • 1
    In Windows XP I had problems and gave me an error "Go to was unexpected at this time". Solution was to put comparison in quotes: if /i "%processor_architecture%"=="AMD64" GOTO AMD64 – Nick Nov 29 '13 at 17:38
  • 1
    Isn't this simpler? `if /i "%processor_architecture%"=="x86" ( rem 32 ) else ( rem 64 )` – mcont Dec 06 '14 at 13:09
  • This will break on any other architecture and execute the 64bit code! There was Itanium and there will probably be Windows on ARM soon! – Josef Dec 06 '14 at 16:29
5
uname -a #for mac

uname -i #for ubuntu
ajmartin
  • 2,379
  • 2
  • 26
  • 42
3

And easy way would be to test for the existence of the %SystemRoot%\SysWOW64 folder. While it's not 100% foolproof, it's a really good for detecting if the system is 64-bit.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • The reason being is that im creating a customisation file for Autocad that our staff can also take home for there personal systems. our customisations make use of VBA for AutoCAD but there are two versions available the 32-bit athe 64-bit. I know that staff make use of both 32 or 64 bits – philip Feb 14 '11 at 10:08
2

All batch up there not work in my Windows 8 x64.

Following work with me :

@cd %programfiles(x86)%\

@if %ERRORLEVEL% == 0 (echo x64&&pause)

@if %ERRORLEVEL% == 1 (echo x86&&pause)
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
1

This Line Will give you what you want, Works on XP, Vista and 7

save it as .bat or .cmd

If Defined ProgramFiles(x86) (\\Fileserver\Distribution\Softwarex64.exe) else (\\Fileserver\Distribution\Softwarex86.exe)

If the Installation Source in the local Machine just point to it (D:\Programs\Softwarex64.exe)

and if you want to just run commands and not to install, just type the command you want for x64 Between the first () and the commands for x86 Between the second ()

If Defined ProgramFiles(x86) (ipconfig /all & @echo  This Is A 64-bit System ) else (arp -a & @echo This Is A 32-bit System)

Copy this into your CMD to test it

I hope this helps

Match
  • 21
  • 2
0

The below method should be pretty reliable since it will work even if environment variables have been messed with:

rem If no kernel32.dll in System32, probably running on DOS or 16-bit Windows
if not exist "%SystemRoot%\System32\kernel32.dll" goto DOS

rem If no kernel32.dll in SysWOW64, likely a 32-bit Windows 
if not exist "%SystemRoot%\SysWOW64\kernel32.dll" goto WIN32

rem If file size reported for kernel32.dll located in System32 and SysWOW64 is
rem the same, it likely means that System32 is being redirected to SysWOW64.
rem This would be the case for 32-bit version of cmd.exe running on 64-bit OS. 
for %%I in ("%SystemRoot%\SysWOW64\kernel32.dll") do (
  for %%J in ("%SystemRoot%\System32\kernel32.dll") do (
    if "%%~zI" equ "%%~zJ" goto WOW64
  )
)

rem If we get this far, the script is likely running in native 64-bit console
echo Native shell on 64-bit Windows
rem ...
exit /b

:WOW64
echo 32-bit shell on 64-bit Windows (WOW64)
rem ...
exit /b

:WIN32
echo 32-bit Windows
rem ...
goto END

:DOS
echo DOS or 16-bit Windows
rem ...
goto END

rem ...

:END
rem We can put this label at the end of the file to allow exiting script on 
rem older systems that do not support 'exit /b'

This method relies on the fact that "%WINDIR%\System32\kernel32.dll" should be present on all Windows systems. 64-bit versions of Windows also include "%WINDIR%\SysWOW64" directory containing 32-bit versions of system files, which is not present on 32-bit systems.

On 64-bit systems, 32-bit applications are redirected to SysWOW64 when trying to access files in System32. So, if we get same size kernel32.dll from both System32 and SysWOW64, it means that redirection is in effect and our script is running in 32-bit console on 64-bit OS.

R.G.
  • 131
  • 1
  • 4
0

On Linux, you can simply use "arch" at the command line.

ubuntu# arch
x86_64

On OSX (Snow Leopard), it returns "i386", even if you're on 64-bit hardware.

Preston Lee
  • 584
  • 1
  • 4
  • 10