在命令提示符下检查String中每个输出行的子字符串

时间:2019-06-08 13:39:02

标签: cmd wmi wmic

Get command prompt out on one linehttps://stackoverflow.com/users/2152082/stephan的支持下 我可以在一行上获得输出,但是现在我遇到了有关在命令提示符下为每个输出行检查String中的子字符串的问题。

我要这样输出:

HDD0 Samsung EVO 12345678字节

USB1 Apacer Sticky 77777777777字节

HDD2 Western 33442134566字节

for /f "skip=2 tokens=2 delims=," %%a in (' "wmic path Win32_DiskDrive get MediaType /format:csv" ') do (
    echo %%a
    REM I need check if it containing string Removable
    if "%%a"=="Removable" (
        echo is-USB!.
        for /f "skip=2 tokens=2,3,4 delims=," %%a in ('"wmic path Win32_DiskDrive get DeviceID,Model,Size /format:csv"') do (
            REM I need repacle "\\.\PHYSICALDRIVE" to "" in %%a
            echo USB%%a %%b - %%c >> "Disk.txt"
        )
    ) else (
        echo is-HDD!.
        for /f "skip=2 tokens=2,3,4 delims=," %%a in ('"wmic path Win32_DiskDrive get DeviceID,Model,Size /format:csv"') do (
            REM I need repacle "\\.\PHYSICALDRIVE" to "" in %%a
            echo HDD%%a %%b - %%c >> "Disk.txt"
        )
    )
)

输出为空。请帮我。 谢谢。

2 个答案:

答案 0 :(得分:0)

Cmd.exe的字符串处理量有限。这是一个很好的咨询站点。 https://www.dostips.com/DtTipsStringManipulation.php

C:>set "S=MACH001,\\.\PHYSICALDRIVE0,WDC WD1003FZEX-00MK2A0,1000202273280"
C:>set "T=%S:\\.\PHYSICALDRIVE=%"
C:>echo %T%
MACH001,0,WDC WD1003FZEX-00MK2A0,1000202273280

答案 1 :(得分:0)

del /f /q "Disk.txt"
for /f "skip=2 tokens=2,3,4 delims=," %%a in ('"wmic path Win32_DiskDrive get DeviceID,Model,Size /format:csv"') do (
    set "S=%%a %%b - %%c"
    set "T=%S:\\.\PHYSICALDRIVE=%"
    echo %T% >> "Disk.txt"
)

“ Disk.txt”不存在。我在哪里弄错了?。