Ben
Ben

Reputation: 57287

Batch file on flash drive - how to check OS architecture?

I'm setting up a portable development environment. I'm trying to get the bitness of the current system my flash drive is plugged into (32 or 64) from a batch file, so I can use the correct version of my IDE.

This article is a start: http://support.microsoft.com/kb/556009 but it uses a relative address, and of course my flash drive doesn't have an OS so the code defaults to i586 every time.

What's the LOC I need to do this?

Upvotes: 2

Views: 1263

Answers (4)

user1354557
user1354557

Reputation: 2503

There is a caveat to Tobias Schlegel's solution: The PROCESSOR_ARCHITECTURE environment variable only returns the bitness of the current process. On a 64-bit machine, PROCESSOR_ARCHITECTURE will still be "x86" in 32-bit processes, due to WoW64 emulation.

To remedy this, Microsoft added a new environment variable, PROCESSOR_ARCHITEW6432, which is only defined in processes running under WoW64.

The correct code is therefore:

if "%PROCESSOR_ARCHITECTURE%" == "x86" if "%PROCESSOR_ARCHITEW6432%" == "" goto Arch32
goto Arch64

:Arch32
echo System architecture is 32-bit!
goto:eof

:Arch64
echo System architecture is 64-bit!
goto:eof

This distinction is important because if you launch cmd.exe from a 32-bit process on a 64-bit machine, then cmd.exe will be running under WoW64, and the accepted solution would therefore be incorrect.

Upvotes: 2

user240217
user240217

Reputation: 132

You can this piece of code (registry):

Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0

REG.exe Query %RegQry% 2>NUL | find /I /N "x86">NUL

If [%ERRORLEVEL%] == [0] (
    echo X86
) ELSE (
    echo AMD64
)

Upvotes: 0

Tobias Schlegel
Tobias Schlegel

Reputation: 3970

just check the PROCESSOR_ARCHITECTURE environment variable on my 64-bit machine it's "AMD64", i guess on a 32bit machine it's "x86".

Upvotes: 2

Alex Churchill
Alex Churchill

Reputation: 4957

wmic OS get OSArchitecture

Should return either 32-bit or 64-bit.

Upvotes: 1

Related Questions