【问题标题】:Is it possible to read the next line of a file only if a particular IF statement is valid?只有当特定的 IF 语句有效时,是否可以读取文件的下一行?
【发布时间】:2013-02-26 14:40:51
【问题描述】:

我想知道,如果某个 IF 语句在其中有效,是否可以跳过读取 FOR /F 循环中的一行? 例如:

FOR /F "tokens=* delims=" %%j in (exclusions.txt) do ( 
IF %SomethingIDeclaredBefore%==%%j (jump to the next line of the text file) else (echo not equal)

我想不可能增加%%j.

【问题讨论】:

    标签: batch-file line counter increment next


    【解决方案1】:

    你在正确的轨道上。 只需执行一个 if 语句来检查 if not %SomethingIDeclaredBefore%==%%j。这样只有与语句不匹配的行才会在循环中处理,本质上是跳过该行。

    FOR /F "tokens=* delims=" %%j in (exclusions.txt) do ( 
        IF NOT %SomethingIDeclaredBefore%==%%j (echo not equal)
    )
    

    更新

    如果我从您的评论中了解您的情况,这应该可以满足您的需求。要检查每个地址的排除,完整的排除循环必须在地址循环内。

    SETLOCAL EnableExtensions EnableDelayedExpansion
    FOR /L %%C IN (1,1,254) DO (
        SET "Found=false"
        FOR /F "tokens=* delims=" %%J IN (exclusions.txt) DO (
            IF "172.24.104.%%C"=="%%J" SET "Found=true"
        )
        IF "!Found!"=="true" ( ECHO Skip ) ELSE ( ECHO Shutdown )
    )
    ENDLOCAL
    

    【讨论】:

    • 感谢您的回答!我应该更清楚我的问题的意思,所以让我介绍一下背景:我必须比较两个ip地址,在2个enmeshed FOR循环中:注意:exclusions.txt充满了受限ip地址FOR /F "tokens=* delims=" %%j in (exclusions.txt) do ( FOR /L %%C in (1,1,254) do ( echo 172.24.104.%%C echo %%j IF NOT 172.24.104.%%C==%%j (echo Shutdown) PAUSE如果两个地址匹配,然后什么也不做,将 exclude.txt 中的%%j 行跳过到下一个,否则,使用此%%j ip 地址关闭计算机。关机应在%%j 上运行。
    【解决方案2】:

    怎么样

    for /l %%C in (1,1,254) do (
     findstr /b /e "172.24.104.%%C" exclusions.txt >nul
     if errorlevel 1 (echo shutdown) else (echo skip)
    )
    

    【讨论】:

    • 太棒了。你让我开心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    相关资源
    最近更新 更多