为什么添加一个if阻止了它的工作

时间:2018-10-25 19:42:28

标签: batch-file wmic

所以我有一个批处理文件,我正在尝试查找C:驱动器的驱动器和分区号。如果我注释掉封装的“ if”,则代码块可以正常工作,但与此同时,它会失败。任何帮助将不胜感激!

@echo off
set DISK=
set PART=

if [%DISK%]==[] (
    rem Get the hard drive information by first searching for and isolating just the C: in the output
    for /f "tokens=1 delims==" %%a in ('wmic Path Win32_LogicalDiskToPartition Get Antecedent^,Dependent /Format:list ^| find /n "=" ^| find "C:"') do set BOOT=%%a
    rem remove the preceeding "[" character
    set BOOT=%BOOT:[=%
    rem remove the appended "]Dependent" string so now we have just the index value of the C:
    set BOOT=%BOOT:]Dependent=%
    rem subtract, by 1, the line indicating the C: so we can now only parse the line that preceeds it in the output to get the actual drive and partition data!
    set /a BOOT=%BOOT%-1
    rem now call the 'wmic' command again but with the prior line being stored for parsing
    for /f "tokens=3 delims==" %%a in ('wmic Path Win32_LogicalDiskToPartition Get Antecedent^,Dependent /Format:list ^| find /n "=" ^| find "[%BOOT%]"') do set DRIVE=%%a
    rem Remove all the quotes from the results
    set DRIVE=%DRIVE:"=%
    for /f "tokens=1,2 delims=," %%a in ("%DRIVE%") do (
        set DISK=%%a
        set PART=%%b
    )
    set DISK=%DISK:Disk #=%
    set PART=%PART: Partition #=%
    if [%DISK%]==[] (
        echo ERROR: You must have a disk number for C: to continue!
        goto ERR
    )
    if [%PART%]==[] (
        echo ERROR: You must have a partition number for C: to continue!
        goto ERR
    )
    echo    Obtained Drive Info: %DRIVE%
)
pause

0 个答案:

没有答案