for循环中goto命令有什么问题?

时间:2014-08-03 09:28:51

标签: batch-file goto

我有一个计算机列表,我想根据tcp连接状态做一些事情 我试图检查tcp连接,如果错误日志是" 1"写行记录并跳到下一台计算机。
问题是,当计算机没有tcp连接时,goto skip_action命令将脚本结束并退出,列表中的其他计算机保持未处理状态。 我还尝试使用goto :eof,它会使脚本意外终止。

ipst.txt file:
1     10.1.1.10
3     10.1.3.10
8     10.1.3.10

这是批处理文件代码:

@echo off
setlocal enabledelayedexpansion 
set computerslist=ipst.txt

for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
    cls
    set Station_Num=%%A
    set Comp_IP=%%B
    ping !Comp_IP! -n 1 | findstr "TTL"     
    if !errorlevel!==1 (
        echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%
        goto skip_action
    )
    echo Getting Administrator Credentials:
    net use \\!Comp_IP! /USER:WORKGROUP\****** ******
    echo.
    xcopy file.txt  \\!Comp_IP!\c\temp\
    echo Disconneting Session From Remote Computer :
    net use \\!Comp_IP! /DELETE /YES    
    :skip_action
    echo end of working on !Station_Num!
)

echo end of script

3 个答案:

答案 0 :(得分:2)

这里的问题是GOTO取消了for循环 但您可以简单地将您的操作包含在ELSE块中


for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
    cls
    set Station_Num=%%A
    set Comp_IP=%%B
    ping !Comp_IP! -n 1 | findstr "TTL"     
    if !errorlevel!==1 (
        echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%

    ) Else (
    echo Getting Administrator Credentials:
    net use \\!Comp_IP! /USER:WORKGROUP\****** ******
    echo.
    xcopy file.txt  \\!Comp_IP!\c\temp\
    echo Disconneting Session From Remote Computer :
    net use \\!Comp_IP! /DELETE /YES    
    )
    echo end of working on !Station_Num!
)

答案 1 :(得分:0)

if - else语句的一般形式

   If condition (

   Statements

   ) else (

   Statements to be executed if condition is not met 

   )

答案 2 :(得分:0)

从循环中拨打电话

你可以有一个函数ping ..并解析它的变量引用..前面的代码也可以呈现为%% a和%% b分别是第一个和第二个参数 - 在函数调用中...

   Call :pingcomputers %%a %%b

这样你就可以完全避免设置

   :pingcomputers

Rem Pinging computer%~1

   Ping %~2 ¦ find "reply"
   If errorlevel 1 goto failed
   (Actions to be performed if ping wa successful)

假设您的脚本标签失败,并且如果错误级别不是0,则执行操作。

不是很多剧本,但希望它有所帮助...

相关问题