Output of wmic is encoded in UTF-16 Little Endian which causes troubles on parsing by command FOR.
The command FOR interprets the end of the file on conversion from Unicode to single byte encoded text as carriage return carriage return line feed. The erroneous carriage return makes the last line not empty. In the provided batch code this results in IF NOT "%%G" == ""
being true as loop variable contains a carriage return. But command SET on evaluating the arithmetic expression interprets the carriage return as whitespace character and therefore there is indeed missing the second operand on addition.
But all this can't be seen on running the batch file in a command prompt window with @echo on
in first line as on write to console window every carriage return is removed by stream writer.
For more details see the answers on How to correct variable overwriting misbehavior when parsing output.
One workaround is to first redirect output of wmic into a temporary file and use command TYPE within FOR to process the lines as command TYPE makes a better job on conversion from Unicode to OEM than command FOR. The temporary file is deleted after processing the lines.
@echo off
setlocal EnableDelayedExpansion
%SystemRoot%\System32\wbem\wmic.exe cpu get NumberOfLogicalProcessors >"%TEMP%\NumberOfLogicalProcessors.tmp"
set "NoOfCores=0"
for /F "skip=1" %%G in ('type "%TEMP%\NumberOfLogicalProcessors.tmp"') do set /A NoOfCores+=%%G
del "%TEMP%\NumberOfLogicalProcessors.tmp"
echo NoOfCores: %NoOfCores%
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
endlocal /?
for /?
set /?
setlocal /?
type /?
wmic cpu get /?