Set SysInfo to Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Set SysInfo to Variable

#1 Post by booga73 » 05 Jan 2014 14:02

How do you set this to a variable instead of piping to text file?

systeminfo | findstr /B /C:"OS Name" /C:"OS Version"

v/r Booga73

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Set SysInfo to Variable

#2 Post by aGerman » 05 Jan 2014 14:41

The output of a command line can be processed in a FOR /F loop.
In your case probably this way:

Code: Select all

for /f "tokens=1,2* delims=: " %%i in (
  'systeminfo ^|  findstr /B /C:"OS Name" /C:"OS Version"'
) do set "%%i_%%j=%%k"
echo %OS_Name%
echo %OS_Version%

I can't test it since my German output of systeminfo is different.

Regards
aGerman

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Set SysInfo to Variable

#3 Post by booga73 » 05 Jan 2014 15:05

Hi aGerman,

thank you,

I'll test it out and let you know; you got me in the right track and will post.

v/r Booga73

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Set SysInfo to Variable

#4 Post by booga73 » 05 Jan 2014 16:03

Absolutely; the code does exactly what I was requesting; thank you aGerman!

v/r Booga73

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Set SysInfo to Variable

#5 Post by booga73 » 06 Jan 2014 15:13

update; for some reason initial inquiry to my question, setting up a command to display on console, I didn't want to pipe to text or log file.
also, I had trouble trying to conceptualize how I wanted to state in batch code what I needed. thanks!

v/r Booga73

the following command, modified from your initial input aGerman:

Code: Select all

for /f "tokens=*" %a in ('systeminfo ^| findstr /B /C:"OS Name" /C:"OS Version" /C:"System Type" /C:"System Boot Time" /C:"Original Install Date" /C:"Total Physical Memory" /C:"Available Physical Memory" /C:"BIOS Version"') do @echo %a


displays the following:

OS Name: Microsoft Windows 7 Enterprise
OS Version: 6.1.7601 Service Pack 1 Build 7601
Original Install Date: 11/19/2012, 12:00:39 PM
System Boot Time: 1/2/2014, 3:19:48 PM
System Type: X86-based PC
BIOS Version: Dell Inc. A06, 7/11/2011
Total Physical Memory: 3,317 MB
Available Physical Memory: 1,461 MB



Yes, you did help me; I'm simply had trouble trying to conceptialize how I needed to setup the command.


Modifying the script even more:

Code: Select all


for /f "tokens=3-6" %a in ('systeminfo ^| findstr /B /C:"OS Name" /C:"OS Version" /C:"System Type" /C:"System Boot Time" /C:"Original Install Date" /C:"Total Physical Memory" /C:"Available Physical Memory" /C:"BIOS Version"') do @echo %a %b %c %d



. . . output. . . .

Microsoft Windows 7 Enterprise
6.1.7601 Service Pack 1
Date: 11/19/2012, 12:00:39 PM
Time: 1/2/2014, 3:19:48 PM
X86-based PC
Dell Inc. A06, 7/11/2011
Memory: 3,317 MB
Memory: 1,468 MB


Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Set SysInfo to Variable

#6 Post by Aacini » 06 Jan 2014 15:54

Perhaps is this what you want?

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Assemble the list of desired systeminfo values
:nextValue
   set "name=%~1"
   set "value[%name: =_%]=X"
   shift
if "%~1" neq "" goto nextValue

rem Load the list of values from systeminfo command
for /F "tokens=1* delims=:" %%a in ('systeminfo') do (
   set "name=%%a"
   if defined value[!name: =_!] set "value[!name: =_!]=%%b"
)

rem Show all names with values
set value[

rem Show only the values
for /F "tokens=2 delims==" %%a in ('set value[') do echo %%a

For example:

Code: Select all

test.bat "OS Name" "OS Version" 

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Set SysInfo to Variable

#7 Post by booga73 » 06 Jan 2014 16:42

Aacini,

I copied / pasted your script into test.bat and ran it. I got the following output:

C:\Users\booga73\Documents\MyScripts>test.bat "OS Name" "OS Version"


value[OS_Name]=X
value[OS_Version]=X
X
X


Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Set SysInfo to Variable

#8 Post by Dos_Probie » 06 Jan 2014 18:59

aGerman's code is the only one that works for my OS I get:

Code: Select all

Microsoft Windows 8.1 Pro with Media Center
6.3.9600 N/A Build 9600

With booga's modified code:

Code: Select all

"OS Name was unexpected at this time"

With Aacini's code:

Code: Select all

"value[ =_]X
_]"

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Set SysInfo to Variable

#9 Post by Aacini » 06 Jan 2014 19:12

This is strange. It seems that "if defined" does not correctly check for the existence of a variable when the name is assembled via Delayed Expansion and there are NOT quotes! Anyway, there is a work around; try this version:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Assemble the list of desired systeminfo values
:nextValue
   set "name=%~1"
   set "value[%name: =_%]=X"
   shift
if "%~1" neq "" goto nextValue

rem Load the list of values from systeminfo command
for /F "tokens=1* delims=:" %%a in ('systeminfo') do (
   set "name=%%a"
   for /F %%v in ("!name: =_!") do if "!value[%%v]!" neq "" set "value[%%v]=%%b"
)

rem Show all names with values
set value[

rem Show only the values
for /F "tokens=2 delims==" %%a in ('set value[') do echo %%a


@Dos_Probie: You must put in the parameters the names of the desired values enclosed in quotes (see the example). This is more practical than include they in the code. aGerman's code does not work if the desired value have three words instead of two...

Antonio

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Set SysInfo to Variable

#10 Post by Dos_Probie » 08 Jan 2014 06:10

Thanks for the info Aacini, just overlooked that
@booga73
With wmic you also can get your Proc info..
DP

Code: Select all

for /f "tokens=2 delims==" %%I in (
  'wmic cpu where "manufacturer!=\"Microsoft\" and name is not null" get name /format:list 2^>NUL'
) do echo %%I

Post Reply