更新循环中的变量

时间:2017-10-23 14:12:18

标签: loops batch-file cmd command increment

业余脚本编码器在这里,我正在寻求一些帮助,让我的循环脚本以所需的方式工作。

所以我有一个要ping的9个IP地址列表,它们存储在2个字母后跟1个数字的变量中,如下所示。

我认为我面临两个问题,无论如何我都能看到。 第一个是当试图调用存储在变量中的IP时,它实际上试图ping我想要的变量的名称,而不是我要调用的变量中的IP。

我知道的第二个问题是如何增加变量的数字。 IE如何使“AP1”增加到“AP2”等等。

帮助! :(

以下是我的脚本的简化版本,用于演示我的情况。在完整的脚本中有许多不同的“网站”

echo off
setlocal enabledelayedexpansion
cls

:SET_SITE_ID
echo There are 3 customers set up: [1] [2] [3]
set /p SITE="Enter Site ID: "
goto %SITE%

:VERIFY_SITE_ID
cls
echo                 Site ID set to: %SITE%
echo                       Location: %LOCATION%
echo          The number of AP's is: %AP_QUANTITY%
echo           The controller IP is: %CONTROLLER%
echo                         AP1 IP: %AP1%
if defined AP2 echo                         AP2 IP: %AP2%
if defined AP3 echo                         AP3 IP: %AP3%
if defined AP4 echo                         AP4 IP: %AP4%
if defined AP5 echo                         AP5 IP: %AP5%
if defined AP6 echo                         AP6 IP: %AP6%
if defined AP7 echo                         AP7 IP: %AP7%
if defined AP8 echo                         AP8 IP: %AP8%
if defined SITE_NOTES echo Site Notes: %SITE_NOTES%
choice /C 12 /N /M "Is the above info correct? [1=Proceed to Ping Test] [2=Change it]"
if %errorlevel%==1 goto PING_TEST
if %errorlevel%==2 goto SET_SITE_ID

:PING_TEST
cls
choice /C 12 /N /M "Which test script? [1=Original] [2=Stephan's script]"
if %errorlevel%==1 goto PING_TEST_ORIGINAL
if %errorlevel%==2 goto PING_TEST_STEPHAN

:PING_TEST_ORIGINAL
cls
echo Starting ping test.
echo -------------------------------------------------------------------------------
echo Testing Controller...
ping -n 2 %CONTROLLER%
echo -------------------------------------------------------------------------------
echo Testing AP1 (of %AP_QUANTITY%)...
ping -n 2 %AP1%
if not defined AP2 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP2 (of %AP_QUANTITY%)...
ping -n 2 %AP2%
if not defined AP3 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP3 (of %AP_QUANTITY%)...
ping -n 2 %AP3%
if not defined AP4 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP4 (of %AP_QUANTITY%)...
ping -n 2 %AP4%
if not defined AP5 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP5 (of %AP_QUANTITY%)...
ping -n 2 %AP5%
if not defined AP6 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP6 (of %AP_QUANTITY%)...
ping -n 2 %AP6%
if not defined AP7 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP7 (of %AP_QUANTITY%)...
ping -n 2 %AP7%
if not defined AP8 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP8 (of %AP_QUANTITY%)...
ping -n 2 %AP8%
if not defined AP9 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP9 (of %AP_QUANTITY%)...
ping -n 2 %AP9%
goto END_OF_PING_TEST

:PING_TEST_STEPHAN
set count=0
for /f "tokens=2 delims==" %%a in ('set AP') do (
set /a count+=1
echo -------------------------------------------------------------------------------
echo testing %%a, [!count! of %AP_QUANTITY%]
ping -n 2 %%a
)
goto END_OF_PING_TEST

:END_OF_PING_TEST
echo -------------------------------------------------------------------------------
choice /C YN /M "End of ping test. Repeat?"
if %errorlevel%==1 goto PING_TEST
if %errorlevel%==2 exit

:1
set LOCATION=Customer 1
set AP_QUANTITY=1
set CONTROLLER=192.168.0.253
set AP1=192.168.0.252
set SITE_NOTES=Notes go here
goto VERIFY_SITE_ID

:2
set LOCATION=Customer 2
set AP_QUANTITY=2
set CONTROLLER=192.168.0.253
set AP1=192.168.0.252
set AP2=192.168.0.111
set SITE_NOTES=Notes go here
goto VERIFY_SITE_ID

:3
set LOCATION=Customer 3
set AP_QUANTITY=9
set CONTROLLER=192.168.0.253
set AP1=192.168.0.252
set AP2=192.168.0.111
set AP3=192.168.0.8
set AP4=127.0.0.1
set AP5=127.0.0.1
set AP6=127.0.0.1
set AP7=127.0.0.1
set AP8=127.0.0.1
set AP9=127.0.0.1
set SITE_NOTES=Notes go here
goto VERIFY_SITE_ID

1 个答案:

答案 0 :(得分:0)

只需进行少量更改:使用延迟展开和更正的if语法(==代替=

@echo off
setlocal enabledelayedexpansion
set AP_QUANTITY=8
set AP1=192.168.0.182
set AP2=192.168.0.183
set AP3=192.168.0.184
set AP4=192.168.0.185
set AP5=192.168.0.186
set AP6=192.168.0.187
set AP7=192.168.0.188
set AP8=192.168.0.189
set APS_YET_TO_PING=%AP_QUANTITY%
set AP_TO_PING=AP1

:PING_APS
echo ----------------------------------------------------
echo Testing !%AP_TO_PING%! (of %AP_QUANTITY%)
ECHO ping -n 2 !%AP_TO_PING%!
echo ----------------------------------------------------
set /a AP_TO_PING=%AP%+1
REM above line won't work like you want.
set /a APS_YET_TO_PING=%APS_YET_TO_PING%-1
REM another syntax: set /a APS_YET_TO_PING -=1
echo AP's remaining to ping=%APS_YET_TO_PING%
echo Next AP=%AP_TO_PING%
pause
if "%APS_YET_TO_PING%"=="0" goto END
goto PING_APS

:END
ECHO Ping test complete
pause

以另一种方式向您展示(使用for /f循环),产生一个很好的短代码:
set _ap列出名称以_ap开头的所有变量(注意:使用唯一的开头,有appdata之类的变量,所以我从下划线开始。)

@echo off 
setlocal enabledelayedexpansion
set Quantity=8
set count=0
set _AP1=192.168.0.182
set _AP2=192.168.0.183
set _AP3=192.168.0.184
set _AP4=192.168.0.185
set _AP5=192.168.0.186
set _AP6=192.168.0.187
set _AP7=192.168.0.188
set _AP8=192.168.0.189
for /f "tokens=2 delims==" %%a in ('set _AP') do (
  set /a count+=1
  echo testing %%a, [!count! of %Quantity%]
  echo ping -n 2 %%a
)

(注意:我使用ECHO禁用了两个脚本中的ping命令,以使代码更快地运行并避免ping错误。只需在排除故障后删除ECHO

修改2

尝试以下方法:

...
:PING_TEST_STEPHAN
set count=0
for /f "tokens=2 delims==" %%a in ('set AP') do (
set /a count+=1
echo ----------------------------------------------------------------------
echo testing %%a, [!count! of %AP_QUANTITY%]
ECHO ping -n 2 %%a
)
goto END_OF_PING_TEST
...

查看我使用_AP...代替AP...的原因!
如果您不确定它是如何工作的,请使用echo on进行尝试。 for /?也应该有所帮助。

for /f占据set AP输出的每一行,"tokens=2 delims=="=(标记1)处将其分割时切出第二部分(称为'标记') = AP<n>,令牌2 =“”。这是我使用_AP<n>的原因,因此set _AP仅显示IP,而不是%APPDATA%AP_QUANTITY。在网站定义中也必须改变它。

请注意,您可以使用单行

简化:VERIFY_SITE_ID
for /f "delims=" %%a in ('set AP') do echo          %%a

而不是所有if defined ...行。 (注意:更改为set _AP,原因如上)
或者更接近你的输出:

for /f "tokens=1,2 delims==" %%a in ('set AP') do echo          %%a IP: %%b