Tuesday, November 30, 2010

Windows 32-bit or 64-bit batch helper


There is an updated blog post about this available. See http://texhex.blogspot.com/2016/11/windows-bitness-helper.html

Given the more spreading use of 64-bit Windows (e.g. Windows 7 x64), the life of an administrator gets even more complicated. For example, if you want to silently install a program and need to specify the destination folder, the command is rather simple:
S:\SuperCalc\installs.exe DESTINATION="%ProgramFiles%\SuperCalc"
At runtime, %ProgramFiles% will be expanded to C:\Program Files and you are done. However, if you want to install on a 64-bit Windows, the program should be installed to C:\Program Files (x86) - given that SuperCalc is a 32 bit application.
In this case, the command should read:
S:\SuperCalc\installs.exe DESTINATION="%ProgramFiles(x86)%\SuperCalc"
Of course, this will not work on any non 64-bit machine since there is no variable %ProgramFiles(x86)%. Additionally, the directory layout for Windows itself is... well… strange on a 64-bit computer. C:\WINDOWS\System32 contains 64-bit files (!!), while the correct path for 32-bit files is C:\WINDOWS\SysWOW64.
Since I prefer a simple solution, I have created a batch file that sets some variables and these always point to the correct path, regardless if the system is 32-bit or 64-bit.
The example from above could simply be written like this:
S:\SuperCalc\installs.exe DESTINATION="%PrgFiles%\SuperCalc"
%PrgFiles% will be either be C:\Program Files on a 32-bit Windows or C:\Program Files (x86) when used with a 64-bit Windows.

Rem -------------------------------------------------------------------
Rem ----- 32 / 64-bit Batch Helper by TeX HeX -
if defined ProgramFiles(x86) GOTO WinX64
Rem This seems to be a 32-bit machine
SET IsX64=0
SET PrgFiles=%ProgramFiles%
SET PrgFilesX64=
SET SYSTEM32=%SystemRoot%\system32

SET IsX64=1
SET PrgFiles=%ProgramFiles(x86)%
SET PrgFilesX64=%ProgramFiles%
SET SYSTEM32=%SystemRoot%\SysWOW64
SET SYSTEM64=%SystemRoot%\system32

echo "IsX64" (is x64 Windows): %IsX64%
Rem "0" on x32 - 1 on x64

echo "PrgFiles" (32-bit Program Files): %PrgFiles%
Rem C:\Program Files on x32 - C:\Program Files (x86) on x64

echo "PrgFilesX64" (64-bit Program Files): %PrgFilesX64%
Rem (empty) on x32 - C:\Program Files on x64

echo "System32" (32-bit System Folder): %SYSTEM32%
Rem C:\WINDOWS\System32 on x32 - C:\WINDOWS\SysWOW64 on x64

echo "System64" (64-bit Windows System Folder): %SYSTEM64%
Rem (empty) on x32 - C:\WINDOWS\System32 on x64
Rem -------------------------------------------------------------------

Please take care if you want to use build-in tools like REGEDIT.EXE. Sometimes they are located in different folders on 32 and 64-bit. This is an example for using the 32-bit REGEDIT.EXE:

Rem On x64, the 32-bit Regedit.exe is located inside the \SysWow64 (32bit System) folder
IF "%IsX64%"=="1" "%system32%\regedit.exe" /s "C:\MyReg.reg"

Rem On x32, the 32-bit Regedit.exe is located inside the \Windows folder
IF "%IsX64%"=="0" "%windir%\regedit.exe" /s "C:\MyReg.reg"

These batch files run perfectly on Windows XP (32-bit and 64-bit) as well as Windows 7 (32-bit and 64-bit). However, if you need to have access to special folders like Desktop, Start menu etc. please see this post.

No comments:

Post a Comment