1

I am trying to get the current operating system using this batch file. It is:

@echo off
for /f "skip=1 tokens=1* delims= " %%a in ('wmic path win32_operatingsystem get caption') do set _os=%%a %%b %%c %%d
echo You are running %_os%.
goto :eof

My operating system is windows 7 so I expected it will return:

You are running Microsoft Windows 7 Ultimate.

But it returns you are running %c %d.

Why I am getting this result?

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 1
    See [Why is the error message “Missing operand” output on processing WMIC output and assigning a value to a variable?](https://stackoverflow.com/questions/53546076/) And there are only the loop variables `a` and `b` because of `tokens=1*`. I suggest `for /f "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe path win32_operatingsystem get caption /Value') do set "_os=%%I"` – Mofi Nov 12 '19 at 16:42
  • You are specifying two tokens (`1` and `*`) but you gave four `for` variables... – aschipfl Nov 13 '19 at 00:37

1 Answers1

2

There are a few ways, one already posted by Mofi in the above comment which already demonstrates the main change using /value. You can also use the caption itself as variable name with its value:

@echo off
for /f "delims=" %%a in ('wmic path win32_operatingsystem get caption /value') do set %%a>nul 2>&1
echo %caption%
Gerhard
  • 22,678
  • 7
  • 27
  • 43