Search code examples
for-loopcmdwmic

batch file for cpu query using for /F loop


Need help.

I am trying to run the following command on a batch file to determine if machine is Intel or AMD...

for /F "skip=1 tokens=1" %%i in ('wmic cpu get name') do set chip=%%i

if "%chip%" == "Intel(R)" goto Intel else goto AMD

:Intel echo this is an Intel machine

:AMD echo this is an AMD machine

but I am running to an issue. The output show as follows:

for /F "skip=1 tokens=1" %i in ('wmic cpu get name') do set chip=%i

set chip=Intel(R)

set chip=

Its showing a second chip "set" basically clearing the first result.

What do I need to input in order to have the loop stop after the first response?


Solution

  • After the first line is set, exit the loop with a GOTO.

    @ECHO OFF
    FOR /F "skip=1 tokens=1" %%i IN ('wmic cpu get name') DO (
        SET "chip=%%i"
        GOTO AfterTest
    )
    
    :AfterTest
    
    IF "%chip%" == "Intel(R)" (GOTO Intel) ELSE (GOTO AMD)
    
    :Intel
    echo this is an Intel machine
    ECHO This is Intel.
    GOTO AfterAll
    
    :AMD
    echo this is an AMD machine
    ECHO This is not Intel.
    GOTO AfterAll
    
    :AfterAll
    

    Another way to get the processor name.

    @ECHO OFF
    FOR /F "delims=" %%p IN ('powershell -NoProfile -Command ^(Get-CimInstance -ClassName CIM_Processor^).Name.Split^(' '^)[0]') DO (
        SET "chip=%%p"
    )
    ECHO %chip%