This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
32 or 64
187} Is the batch file called on a 32-bit or 64-bit system?
Originally the question was "Is batch file called from 32-bit or 64-
bit cmd.exe?" There is a lengthy, slightly diverging discussion about
the original subject in the reference at the end of this item. The
solution to the essential modified question, however, is
straight-forward.
if defined ProgramW6432 ...
alternatively
if defined ProgramFiles(x86)
Since some batches use auxiliary executable programs, such batches
will not work in a 64-bit environment if the said executables are
16-bit. At least 32-bit is required. Hence the need of a simple test.
Consequently, you might have something like this:
if defined ProgramW6432 (
echo/
echo Exiting: %~f0 is incompatible with a 64-bit OS
goto :EOF)
More fully, what kind of OS version compatibility tests depends on the
particular contents of the script file. What kind of commands and
maybe auxiliary programs it uses. Two examples. 1) If one utilizes
sed.exe and/or
gawk.exe, very useful UNIX ports, their 16-bit versions will not
run on a 64-system. 2) If one uses
C:\Windows\System32\fsutil.exe in a post-XP command line
script, one will run into addition complications with user privileges
compared with running it on a pre-Windows 7 system. Therefore, one
might want to test, not only for the bit flavor but also for the OS
version. One might include something like this:
@echo off & setlocal enableextensions
if defined ProgramW6432 (
echo/
echo Exiting: %~f0 is incompatible with a 64-bit OS
goto :EOF)
::
ver|find "5.1.">nul
if errorlevel 1 (
echo/
echo Exiting: %~f0 is not for post-XP systems
goto :EOF)
::
echo Whatever would follow ...
::
endlocal & goto :EOF
The the sake of the exercise, the latter could be written as a
subroutine, such as:
@echo off & setlocal enableextensions
::
call :IsPostXP "%~f0" notxp_
if defined notxp_ goto :EOF
::
echo Whatever would follow ...
::
endlocal & goto :EOF
::
:IsPostXP
setlocal enableextensions
set return_=
ver|find "5.1.">nul
if errorlevel 1 (
set return_=true
echo/
echo Exiting: %~f1 is not for post-XP systems)
endlocal & set "%2=%return_%" & goto :EOF
P.S. I am not taking any view here about the 8.4.2014 Microsoft end of
XP support. This item is just about the code, not policy nor security.
I'll leave those to the reader's own discretion.