为什么调用 echo 输出到文件时结果加倍?
Why double results when call echo to output to file?
我正在尝试生成关于 disks/drives 的 DiskDevices.ini 文件。
这是my batch。
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET "InputFile=DiskDevices.ini"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
SET /A "TotalDevices=0"
SET /A "Index=0"
@FOR /F "skip=2 tokens=*" %%G IN ('
"WMIC PATH Win32_DiskDrive GET Index /format:csv 2>NUL"
') DO (
SET /A "TotalDevices=TotalDevices+1"
SET /A "Index=Index+1"
>>"%InputFile%" CALL ECHO [Device_%%Index%%]
@FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskDrive GET Caption,Size /value 2>NUL"
') DO (
@FOR /F Tokens^=* %%H IN ("%%G") DO (>>"%InputFile%" CALL ECHO %%H)
)
)
>>"%InputFile%" CALL ECHO [Total Devices]
>>"%InputFile%" CALL ECHO Value=%%TotalDevices%%
PAUSE
结果循环了两次
[Device_1]
Caption=Samsung EVO
Size=162536803840
Caption=Kingston USB Device
Size=31718510080
[Device_2]
Caption=Samsung EVO
Size=162536803840
Caption=Kingston USB Device
Size=31718510080
[Total Devices]
Value=2
我也需要这个:
[Device_1]
Caption=Samsung EVO
Size=162536803840
[Device_2]
Caption=Kingston USB Device
Size=31718510080
[Total Devices]
Value=2
感谢您的帮助。
因为您获得了两次设备列表。在一个循环中完成所有操作,或将具有 Index
值的 where
条件添加到第二个 wmic
调用以过滤掉其他设备。
好吧,你嵌套了两个 for /F
循环,它们收集的信息基本相同,这就是你得到重复项的原因。
这种方法怎么样,它使用 Index
属性 作为 [Device_#]
header 以及内部 where
子句=15=] 查询:
@echo off
set "COUNT=0"
> "DiskDevices.ini" (
for /F "delims=" %%J in ('wmic DiskDrive get Index /VALUE 2^> nul') do (
for /F "tokens=2 delims==" %%I in ("%%J") do (
echo [Device_%%I]
for /F "delims=" %%H in ('wmic DiskDrive where "Index=%%I" get Caption^,Size /VALUE 2^> nul') do (
for /F "delims=" %%G in ("%%H") do (
echo(%%G
)
)
set /A "COUNT+=1"
)
)
echo [Total Devices]
call echo Value=%%COUNT%%
)
我正在尝试生成关于 disks/drives 的 DiskDevices.ini 文件。
这是my batch。
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET "InputFile=DiskDevices.ini"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
SET /A "TotalDevices=0"
SET /A "Index=0"
@FOR /F "skip=2 tokens=*" %%G IN ('
"WMIC PATH Win32_DiskDrive GET Index /format:csv 2>NUL"
') DO (
SET /A "TotalDevices=TotalDevices+1"
SET /A "Index=Index+1"
>>"%InputFile%" CALL ECHO [Device_%%Index%%]
@FOR /F Tokens^=* %%G IN ('
"WMIC PATH Win32_DiskDrive GET Caption,Size /value 2>NUL"
') DO (
@FOR /F Tokens^=* %%H IN ("%%G") DO (>>"%InputFile%" CALL ECHO %%H)
)
)
>>"%InputFile%" CALL ECHO [Total Devices]
>>"%InputFile%" CALL ECHO Value=%%TotalDevices%%
PAUSE
结果循环了两次
[Device_1]
Caption=Samsung EVO
Size=162536803840
Caption=Kingston USB Device
Size=31718510080
[Device_2]
Caption=Samsung EVO
Size=162536803840
Caption=Kingston USB Device
Size=31718510080
[Total Devices]
Value=2
我也需要这个:
[Device_1]
Caption=Samsung EVO
Size=162536803840
[Device_2]
Caption=Kingston USB Device
Size=31718510080
[Total Devices]
Value=2
感谢您的帮助。
因为您获得了两次设备列表。在一个循环中完成所有操作,或将具有 Index
值的 where
条件添加到第二个 wmic
调用以过滤掉其他设备。
好吧,你嵌套了两个 for /F
循环,它们收集的信息基本相同,这就是你得到重复项的原因。
这种方法怎么样,它使用 Index
属性 作为 [Device_#]
header 以及内部 where
子句=15=] 查询:
@echo off
set "COUNT=0"
> "DiskDevices.ini" (
for /F "delims=" %%J in ('wmic DiskDrive get Index /VALUE 2^> nul') do (
for /F "tokens=2 delims==" %%I in ("%%J") do (
echo [Device_%%I]
for /F "delims=" %%H in ('wmic DiskDrive where "Index=%%I" get Caption^,Size /VALUE 2^> nul') do (
for /F "delims=" %%G in ("%%H") do (
echo(%%G
)
)
set /A "COUNT+=1"
)
)
echo [Total Devices]
call echo Value=%%COUNT%%
)