Windows批处理:如果未验证条件

时间:2017-05-03 04:40:30

标签: batch-file command-line

这是我的代码

ECHO off
CLS
ECHO List of VMs:
ECHO .............
cd /D "F:\VMs"
setlocal enabledelayedexpansion
set num=0
for /f "tokens=*" %%i in ('dir /s/b *.vmx') do set /a num+=1&set VM[!num!]=%%i 
set numberOfVMs=0
for /F "tokens=2 delims==" %%s in ('set VM[') do (
    set /a numberOfVMs+=1
    echo !numberOfVMs! -^> %%s
)
ECHO.
ECHO List of Running VMs:
ECHO .....................
set num=-1
for /f "tokens=*" %%i in ('vmrun -T ws list') do (
    set /a num+=1
    if !num! NEQ 0 set RUNNINGVM[!num!]=%%i
)
set numberOfRunningVMs=0
for /F "tokens=2 delims==" %%s in ('set RUNNINGVM[') do (
    set /a numberOfRunningVMs+=1
    echo !numberOfRunningVMs! -^> %%s
)
ECHO.
ECHO List of Available VMs:
ECHO .......................
for /l %%x in (1, 1, %numberOfVMs%) do ( 
    ::echo !VM[%%x]!
    for /l %%y in (1, 1, %numberOfRunningVMs%) do (
            echo !VM[%%x]!
            echo !RUNNINGVM[%%y]!
        if "!VM[%%x]!"=="!RUNNINGVM[%%y]!" (
            echo "success"
        )
    )
)

数组varibale'VM'具有以下值:

F:\虚拟机\ PD-UI-试验 - Win10-x64的Office2016-32bit.vmwarevm \ Windows的10-x64.vmx

F:\虚拟机\ PD-UI-试验 - Win10-x64的Office2016-64bit.vmwarevm \ Windows的10-x64.vmx

F:\虚拟机\ PD-UI-试验 - Win7的-x64的Office2013-32bit.vmwarevm \ Windows7的-x64的RTM.vmx

数组varibale'RUNNINGVM'具有以下值:

F:\虚拟机\ PD-UI-试验 - Win7的-x64的Office2013-32bit.vmwarevm \ Windows7的-x64的RTM.vmx

但是,尽管VM [3]等于RUNNINGVM [1],但最终if条件永远不会变为'true'。我错过了什么?请帮忙

1 个答案:

答案 0 :(得分:0)

不要在代码块中使用:: - 样式注释,因为它实际上是一个损坏的标签,标签会破坏代码块。请改用rem