Array的成员未正确评估

时间:2013-05-10 18:33:51

标签: batch-file

我今天写了下面的脚本以回答另一个问题,但我无法解决我在使用Array时出现的问题。由于某种原因,我在数组内部使用的变量没有正确评估。这是我的代码:

@echo off
setlocal enabledelayedexpansion

set q=wmic /NAMESPACE:\\root\directory\ldap PATH ds_computer get ds_name
set q1=NICCONFIG get DNSHostname^,IPAddress^,MACAddress
set /a "c=1" & set /a "inc=1" & set /a "inc2=0"
::for /f "skip=1" %%a in ('%q%') do (
    for /f "tokens=1-3 delims=] " %%j in (
      'wmic logicaldisk where drivetype^=3 get deviceid^,volumeserialnumber^|find /N ":"') do (
            set "lines=%%j"
            set "v[!c!]=%%k" & set /a "c+=1"
            set "v[!c!]=%%l" & set /a "c+=1"
            if [!lines! equ [!c! goto :out
     )
    :out
    set c=
    for /f "tokens=1 delims=|" %%i in (
       'wmic /node:"wpcf-dc" OS get name^|find "|"') do (set v=%%i)
    for /f "tokens=1-3 delims={}" %%b in (
       'wmic /NODE:"wpcf-dc" NICCONFIG Where IPEnabled^=TRUE get DNSHostname^,IPAddress^,MACAddress /FORMAT:TABLE^|find "{"'
      ) do ( For /F "tokens=1-3 delims= " %%e in ("%%~b %%~c %%d") do (
       echo(Host: %%e
       echo(IP: %%f
           echo(MAC: %%g
       echo(Windows Version: !v!
       set lines=!lines:[= !
       for /L %%l in (1,2,!lines!) do (
           set /a "inc2=%%l" & set /a "inc2+=1"
           echo value of inc2=!inc2!
           echo(Drive!inc!: !v[%%l]!
           echo(Volume!inc!: !v[!inc2!]!
           set /a "inc+=1"
       )     
      ) 
    )
 ::)

这是我得到的输出。

C:\Scripts\Batch>LDAPquery.cmd
Host: WPCF-DC
IP: 10.6.1.226
MAC: 00:11:43:EC:D4:B7
Windows Version: Microsoft Windows Server 2003 Standard Edition
value of inc2=2
Drive1: C:
Volume1: inc2
value of inc2=4
Drive2: D:
Volume2: inc2

我不知道为什么当它的值是数字时,不会在数组内部对inc2进行求值。任何见解将不胜感激。

2 个答案:

答案 0 :(得分:2)

这是我的代码版本:

@echo off
setlocal enabledelayedexpansion

set q=wmic /NAMESPACE:\\root\directory\ldap PATH ds_computer get ds_name
set q1=NICCONFIG get DNSHostname,IPAddress,MACAddress
set /a c=1, inc=1, inc2=0
::for /f "skip=1" %%a in ('%q%') do (
    for /f "tokens=1-3 delims=] " %%j in (
      'wmic logicaldisk where drivetype=3 get deviceid,volumeserialnumber^|find /N ":"') do (
        set "lines=%%j"
        set "v[!c!]=%%k" & set /a "c+=1"
        set "v[!c!]=%%l" & set /a "c+=1"
        if "!lines!" equ "!c!" goto :out
    )
    :out
    set c=
    for /f "tokens=1 delims=|" %%i in (
      'wmic /node:"wpcf-dc" OS get name^|find "|"') do "set v=%%i"
    for /f "tokens=1-3 delims={}" %%b in (
      'wmic /NODE:"wpcf-dc" NICCONFIG Where IPEnabled=TRUE get DNSHostname,IPAddress,MACAddress /FORMAT:TABLE^|find "{"') do (
        for /F "tokens=1-3" %%e in ("%%~b %%~c %%d") do (
            echo Host: %%e
            echo IP: %%f
            echo MAC: %%g
            echo Windows Version: !v!
            set lines=!lines:[= !
            for /L %%l in (1,2,!lines!) do (
                set /a inc2=%%l+1
                echo value of inc2=!inc2!
                echo Drive!inc!: !v[%%l]!
                for %%v in (!inc2!) do echo Volume!inc!: !v[%%v]!
                set /a "inc+=1"
            )     
        ) 
    )
::)

我认为这一行不得出现:

set c=

我建议你在嵌套的IF / FOR命令中严格使用理由列;这种做法有助于找到未闭合的括号。另外,不要在不需要时使用括号。

请注意,此命令echo Volume!inc!: !v[!inc2!]!不能以这种方式执行,因为感叹号的解析是从左到右实现的,所以前一行意味着:echo“Volume”+“inc”+“:”的值+“v [”+“inc2”+值“]”的值。您必须在FOR命令中实现!inc2!的扩展,然后在第二个扩展中使用可替换参数:

for %%v in (!inc2!) do echo Volume!inc!: !v[%%v]!

您可以查看this post以获取前一点的进一步说明。

您必须注意,如果激活顶级for /f "skip=1" %%a in ('%q%') do (,则执行GOTO时goto :out命令会将其中断。打破嵌套for /f "tokens=1-3 delims=] " %%j in ( 'wmic ...命令的方法是通过这种方式改变它:

for /f "tokens=1-3 delims=] " %%j in ('wmic ... ') do if "!lines!" neq "!c!" (

并消除if ... goto :out:out标签。

答案 1 :(得分:1)

试试这个:

@echo off
set "v[2]=MyVolume"
set inc2=2
setlocal enabledelayedexpansion
set /a inc=1
for /L %%l in (1,2,2) do (
   set /a inc2=%%l+1
   echo value of inc2=!inc2!
   echo(Volume!inc!: !v[!inc2!]!
   set /a "inc+=1"
)     
set /a inc=1
for /L %%l in (1,2,2) do (
   set /a "inc2=%%l+1
   echo value of inc2=!inc2!
   call echo(Volume!inc!: %%v[!inc2!]%%
   set /a "inc+=1"
)     

输出是:

value of inc2=2
Volume1: inc2
value of inc2=2
Volume1: MyVolume