使用for / F循环进行cpu查询的批处理文件

问题描述 投票:1回答:1

需要帮忙。

我试图在批处理文件上运行以下命令,以确定机器是Intel还是AMD ......

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

如果“%chip%”==“英特尔(R)”转到英特尔其他转到AMD

:Intel echo这是一款Intel机器

:AMD回声这是一台AMD机器

但是我遇到了一个问题。输出显示如下:

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

设置芯片=英特尔(R)

设置芯片=

它显示第二个芯片“设置”基本上清除了第一个结果。

我需要输入什么才能在第一次响应后停止循环?

for-loop cmd wmic
1个回答
0
投票

设置第一行后,使用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

获取处理器名称的另一种方法。

@ECHO OFF
FOR /F "delims=" %%p IN ('powershell -NoProfile -Command ^(Get-CimInstance -ClassName CIM_Processor^).Name.Split^(' '^)[0]') DO (
    SET "chip=%%p"
)
ECHO %chip%
© www.soinside.com 2019 - 2024. All rights reserved.