Page 1 of 1
Set SysInfo to Variable
Posted: 05 Jan 2014 14:02
by booga73
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
Re: Set SysInfo to Variable
Posted: 05 Jan 2014 14:41
by aGerman
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
Re: Set SysInfo to Variable
Posted: 05 Jan 2014 15:05
by booga73
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
Re: Set SysInfo to Variable
Posted: 05 Jan 2014 16:03
by booga73
Absolutely; the code does exactly what I was requesting; thank you aGerman!
v/r Booga73
Re: Set SysInfo to Variable
Posted: 06 Jan 2014 15:13
by booga73
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
Re: Set SysInfo to Variable
Posted: 06 Jan 2014 15:54
by Aacini
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:
Re: Set SysInfo to Variable
Posted: 06 Jan 2014 16:42
by booga73
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
Re: Set SysInfo to Variable
Posted: 06 Jan 2014 18:59
by Dos_Probie
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:
Re: Set SysInfo to Variable
Posted: 06 Jan 2014 19:12
by Aacini
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
Re: Set SysInfo to Variable
Posted: 08 Jan 2014 06:10
by Dos_Probie
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