批处理 - 循环通过逗号分隔的字符串

Batch - Loop through comma delimited string

我正在尝试遍历 wmic 的字段,然后将其输出写入文本文件。

最终的目标是拥有一个制表符(或其他)分隔的硬件信息文件。我似乎无法通过第一个命令循环,执行在每个循环的第一次执行时结束。

以前没怎么处理批处理文件,请帮我解决这个问题。

::@ECHO OFF

:: Prompt user for computer name
SET /P id="Enter Computer Name: "

::Fetch IP
FOR /F "skip=4 usebackq tokens=2" %%a IN (`nslookup %id%`) DO (
SET ip=%%a
)

::Change working directory to .bat file location
CD %~dp0

::Creates/Clears record.
break > %id%.txt

REM Level 1: Loops through fields to be pulled
FOR /F "tokens=1* delims=," %%c IN ("computersystem get name,computersystem get name,computersystem get name") DO (

    REM Level 2: Pulls value in key=value format, parses value by '=' delimiter
    FOR /F "tokens=2 delims==" %%x IN ('
        wmic %%c /value
    ') DO (
        REM Level 3: Removes line carriage, adds a <tab> character. 
        FOR /f %%z IN ("%%x") DO (
            <nul set /p = %%z
            <nul set /p = " "
        )>> %id%.txt
    )

)

您似乎在 Level 1: 使用 For /F 循环而不是标准 For 循环:

@Echo Off
CD /D "%~dp0"
Set /P "ID=Enter Computer Name: "
For /F "Tokens=2" %%A In ('NSLookUp %ID% 2^>Nul') Do Set "IP=%%A"
(For %%A In ("ComputerSystem Get Name","ComputerSystem Get Name",
 "ComputerSystem Get Name") Do For /F "Tokens=1* Delims==" %%B In (
    'WMIC %%~A /Value 2^>Nul') Do For /F "Tokens=*" %%D In ("%%~C"
) Do <Nul Set /P=%%D    )>"%ID%.txt"

注意: 最后一个右括号前的空格是 TAB