【问题标题】:Can there be more than one EOL character in a DOS FOR loop?DOS FOR 循环中可以有多个 EOL 字符吗?
【发布时间】:2014-09-16 20:37:21
【问题描述】:

有谁知道在 DOS 批处理脚本中让多个字符成为“eol”字符的方法。我想同时使用';'和 '#' 作为字符来注释一行。

type t.txt
this is a line
#this is a commented line
;is this a commented line
last line

for /f "usebackq tokens=* eol=; eol=#" %f in (`type t.txt`) do (echo "%f")
"this is a line"
";is this a commented line"
"last line"

for /f "usebackq tokens=* eol=;#" %f in (`type t.txt`) do (echo "%f")
#" was unexpected at this time.

【问题讨论】:

    标签: batch-file for-loop


    【解决方案1】:

    您可以在内部嵌套 for 循环以定义另一个 eol:

    for /f "usebackq eol=; delims=" %%f in (`type t.txt`) do (
        for /f "eol=# delims=" %%Z in ("%%~f")  do echo %%Z
    )
    

    但作为命令行,它可能是一个很长的表达式...

    令我惊讶的是,cmd.exe 并没有抱怨第一个示例中的两个 eol 定义,尽管它只使用了第一个。另一种方法是使用FINDSTR 过滤结果:

    for /f  "usebackq delims=" %%f in (`type t.txt^| findstr /i /b /v "; #"`) do ( echo %%f )
    

    或从命令行:

    for /f  "usebackq delims=" %f in (`type type.txt^| findstr /i /b /v "; #"`) do ( echo %f )
    

    FOR 循环内的 IF 条件也很少,但这需要延迟扩展,并且在命令行中使用不太方便。

    【讨论】:

      【解决方案2】:

      这个命令单独解决了这个问题:

      findstr /V /B "# ;" t.txt
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多