0

I'm having a hard time trying to save the output of the following command, which gets me the name of the OS to a variable.

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

I tried using a for loop as shown below but then the value of the variable is OS

for /f %%i in ('systeminfo ^| findstr /B /C:"OS Name" ') do set vard=%%i
echo the operating system name 2 is %vard%

Can someone please help me out with this ? I took a look at other approaches such as writing the output to a temporary file and then reading it back later on but I'd like to achieve this without resorting to using temporary files

Dhiwakar Ravikumar
  • 1,983
  • 2
  • 21
  • 36
  • 1
    I suggest you to type `FOR /?` in cmd's command prompt and carefully read the help text. there is even an example of how to extract a specific token. The output from running `systeminfo | findstr /B /C:"OS Name"` will be something like this: `OS Name: Microsoft Windows ....` there are multiple tokens delimited by spaces, you will need the rest of the tokens after the second token(`Name:`) – sst Jul 17 '18 at 05:59
  • Possible duplicate of [SystemInfo - Get computer System Model via CMD - Extra spaces bug](https://stackoverflow.com/questions/15973276/systeminfo-get-computer-system-model-via-cmd-extra-spaces-bug) –  Jul 17 '18 at 09:29

2 Answers2

0
@echo off
setlocal

set "vard="

for /f "tokens=1,2,* delims=: " %%i in ('systeminfo') do (
    if "%%~i %%~j" == "OS Name" (
        call :clean_spaces "%%~k"
        goto :next
    )
)

:next
echo the operating system name 2 is "%vard%"
exit /b

:clean_spaces
setlocal enabledelayedexpansion
set "string=%~1"
    :loop
    if "!string:~-1!" == " " (
        set "string=!string:~,-1!"
        goto :loop
    )
endlocal & set "vard=%string%"

Delimit by colon and space. Unfortunately, token * also gets the trailing spaces which systeminfo outputs.

A label named :trim_spaces trims the trailing spaces from the value that vard stores. It does this by checking the last character if a space and removes it. It keeps looping until no trailing spaces exist.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
0

To use the slow SystemInfo method, as used in your posted code, you should delimit the output using the known separator character, :.

@Echo Off
For /F "Tokens=1* Delims=:" %%A In ('SystemInfo^|Find /I "OS Name"'
) Do Call :Sub %%B
Echo the operating system name 2 is %vard%
Pause
GoTo :EOF

:Sub
Set "vard=%*"
Exit /B

Alternatively, you can either use the built-in WMI command line, WMIC.exe.

@Echo Off
For /F "Skip=1 Delims=|" %%A In ('WMIC OS Get Name'
) Do For /F "Delims=" %%B In ("%%A") Do Call :Sub %%A
Echo the operating system name 2 is %vard%
Pause
GoTo :EOF

:Sub
Set "vard=%*"
Exit /B

Or retrieve the information from the registry, using Reg.exe:

@Echo Off
For /F "Skip=2 Tokens=2*" %%A In (
    'Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /V ProductName'
) Do Set "vard=%%B" 
Echo the operating system name 2 is %vard%
Pause
GoTo :EOF
Compo
  • 36,585
  • 5
  • 27
  • 39