For that particular case, you can start with:
for /f "tokens=3" %i in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE ^| findstr PROCESSOR_ARCHITECTURE') do set x=%i
which will set %x%
to x86 (in my case).
If the third value on the line can contain spaces, you'll need to get a bit trickier
The following script shows one way to be trickier. It basically uses a debug line to produce fictional output from reg
that gives you an architecture with spaces.
@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=2*" %%a in ('echo. PROCESSOR_ARCHITECTURE REG_SZ x86 64-bit grunter') do (
set arch=%%b
)
echo !arch!
endlocal
The output of that is:
x86 64-bit grunter
as expected (keep in mind it collapses multiple spaces into one). The tokens=2*
bit puts token 2 (REG_SZ
) into %%a
and all following tokens into %%b
.
So a good final script would be:
@setlocal enableextensions enabledelayedexpansion
@echo off
set id=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
set key=PROCESSOR_ARCHITECTURE
for /f "tokens=2*" %%a in ('REG QUERY "!id!" /v !key! ^| findstr !key!') do (
set arch=%%b
)
echo !arch!
if !arch!==x86 echo arch was x86
endlocal
This script outputs:
x86
arch was x86
so you know it's being set as you desire.