Page 1 of 1

ProcessorId String Correcty?

Posted: 05 Mar 2022 06:39
by Bjorn94
Hi everyone, I have a problem, with this code it should tell me that it is correct, instead it says that it is not, you have to put your processorID inside the quotes, I did not put mine because it would not make sense, someone can tell me why it does not work correctly ?

Code: Select all

@echo off
Set cpuid= wmic cpu get processorid^|more +1
%cpuid%
if NOT "%cpuid%"=="  " (goto A) else (goto B)
:B
echo It worked
pause > nul
:A
echo It does not work
pause > nul

Re: ProcessorId String Correcty?

Posted: 05 Mar 2022 09:23
by aGerman
There is no syntax in batch to just use SET for the assignment of the output of a command. The command has to be executed in a FOR /F loop in order to capture its output. Furthermore, WMIC outputs weird line endings. I'm used to working around it in a second FOR /F.

Code: Select all

@echo off &setlocal
set "cpuid="
for /f "delims=" %%i in ('wmic cpu get ProcessorId /value') do for /f "tokens=2 delims==" %%j in ("%%i") do set "cpuid=%%j"
if not defined cpuid (
  echo Unable to get the processor ID
  pause
  exit /b
)

echo %cpuid%
pause
Steffen

Re: ProcessorId String Correcty?

Posted: 05 Mar 2022 10:05
by Bjorn94
Wow, thanks, N.1 :D :D :D