我正在尝试遍历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
)
)
答案 0 :(得分:1)
您似乎在For /F
使用Level 1:
循环而不是标准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