There is no need for delayed expansion, a temp file, or FINDSTR, and certainly no need to count space delimited tokens. You just need to set the correct DELIMS and TOKENS options. The first FOR /F processes the output of systeminfo
and parses each line into a name and value. The second FOR /F strips the leading white space from each value. All that is left is a series of IF statements to test each name and set the desired variable as appropriate.
@echo off
setlocal
for /f "delims=: tokens=1*" %%A in ('systeminfo') do (
for /f "tokens=*" %%S in ("%%B") do (
if "%%A"=="OS Name" set "OSNAME=%%S"
if "%%A"=="OS Version" set "OSVER=%%S"
if "%%A"=="Original Install Date" set "INSTDATE=%%S"
if "%%A"=="System Manufacturer" set "SYSMFG=%%S"
if "%%A"=="System Model" set "SYSMDL=%%S"
)
)
echo(
echo Operating System : %OSNAME%, %OSVER%
echo Original Install Date : %INSTDATE%
echo Model Information : %SYSMFG% %SYSMDL%
If you want to capture more values, it might be desirable to specify the search string:variable name pairs using strings and add some extra FOR loops to process each test as follows:
@echo off
setlocal
for /f "delims=: tokens=1*" %%A in ('systeminfo') do for %%S in (
"OS Name:OSNAME"
"OS Version:OSVER"
"Original Install Date:INSTDATE"
"System Manufacturer:SYSMFG"
"System Model:SYSMDL"
) do for /f "delims=: tokens=1*" %%a in ("%%~S") do if %%A==%%a (
for /f "tokens=*" %%s in ("%%B") do set "%%b=%%s"
)
echo(
echo Operating System : %OSNAME%, %OSVER%
echo Original Install Date : %INSTDATE%
echo Model Information : %SYSMFG% %SYSMDL%