Question

I am looking for a cleaner way to do this. I'm pretty sure it's wrong even though it kind of works. Basically I'm taking output like this from a file (temp.txt):

    Host Name:                 IBM-3J93A46MRS5
    OS Name:                   Microsoft Windows 7 Professional 
    OS Version:                6.1.7631 Service Pack 1 Build 7601
    OS Configuration:          Standalone Workstation
    Registered Owner:          IAMADMIN
    Original Install Date:     2011-12-15, 10:56:07 AM
    System Boot Time:          2013-05-27, 9:55:36 AM
    System Manufacturer:       DELL
    System Model:              4009BF7
    Time Zone:                 (UTC-05:00) Eastern Time (US & Canada)
    Total Physical Memory:     16,316 MB
    Available Physical Memory: 11,356 MB

And trying to get JUST the 2nd column of data into a variable. Depending on if I want it or not. I'm using Findstr to weed out.

    @echo off
    setlocal enabledelayedexpansion

    systeminfo > temp.txt
    set "tmp_result_file=temp.txt"

    for /f "tokens=3-7 delims= " %%i in ('FINDSTR /C:"OS Name:" %tmp_result_file%') do set "OSNAME=%%i %%j %%k %%l"
    for /f "tokens=3-10 delims= " %%i in ('FINDSTR /V /C:"BIOS Version:" %tmp_result_file%^|FIND /I "OS Version:"') do set "OSVER=%%i %%j %%k %%l %%m %%n %%o %%p"
    for /f "tokens=4-7 delims= " %%i in ('FINDSTR /C:"Original Install Date:" %tmp_result_file%') do set "INSTDATE=%%i %%j %%k %%l"
    for /f "tokens=3-7 delims= " %%i in ('FINDSTR /C:"System Manufacturer:" %tmp_result_file%') do set "SYSMFG=%%i %%j %%k %%l"
    for /f "tokens=3-7 delims= " %%i in ('FINDSTR /C:"System Model:" %tmp_result_file%') do   set "SYSMDL=%%i %%j %%k %%l"

    echo.
    echo Operating System      : %OSNAME% , %OSVER% 
    echo Original Install Date : %INSTDATE%
    echo Model Information     : %SYSMFG% %SYSMDL% 

I don't like that I am using tokens so precisely, I just need everything after the search value.

Was it helpful?

Solution

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%

OTHER TIPS

goi'n the easy way :-)

@echo OFF &setlocal
FOR /f "tokens=1*delims=:" %%i IN (file) DO (
    FOR /f "tokens=*" %%a IN ("%%j") DO SET "$%%i=%%a"
)
SET "$"
echo Windows version: %$OS Name%

..output is:

$Available Physical Memory=11,356 MB
$Host Name=IBM-3J93A46MRS5
$Original Install Date=2011-12-15, 10:56:07 AM
$OS Configuration=Standalone Workstation
$OS Name=Microsoft Windows 7 Professional
$OS Version=6.1.7631 Service Pack 1 Build 7601
$Registered Owner=IAMADMIN
$System Boot Time=2013-05-27, 9:55:36 AM
$System Manufacturer=DELL
$System Model=4009BF7
$Time Zone=(UTC-05:00) Eastern Time (US & Canada)
$Total Physical Memory=16,316 MB
Windows version: Microsoft Windows 7 Professional

If you need to get everything else after the 3rd token, simply use a wildcard in the token value as follows:-

for /f "tokens=3* delims= " %%i in ('FINDSTR /C:"OS Name:" %tmp_result_file%') do set "OSNAME=%%i"

should display

Microsoft Windows 7 Professional
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top