代码之家  ›  专栏  ›  技术社区  ›  dls

Windows批处理文件错误级别问题

  •  0
  • dls  · 技术社区  · 16 年前

    我有一个批处理文件,从文本文件中解析出一堆文件名,并将它们连接成一个单一的强-这是前面讨论过的。 here . 但是,如果文件在我通过某些命令(例如VCS检查)运行时抛出错误,我不希望字符串包含文件。我的尝试是:

    set FILE_LIST=
    for /f %%x in (temp.txt) do (
    
    :: perform a VCS test
    accurev diff -b %%x
    
    :: skip concatenation if error level is > 2
    IF errorlevel 2 GOTO NEXT
    
    :: perform the concatenation
    set FILE_LIST=!FILE_LIST! %%x
    
    :NEXT
    :: print a message if here due to an error above
    IF errorlevel 2 echo VCS problem with this file: %%x
    )
    

    问题是-一旦发现一个错误级别大于2,脚本似乎就会停止执行整个for循环。如果列表中有五个文件,而第三个文件有VCS问题,则脚本只处理前两个文件。

    2 回复  |  直到 16 年前
        1
  •  0
  •   Anders    16 年前
    setlocal ENABLEDELAYEDEXPANSION
    set FILE_LIST=
    for /f %%x in (temp.txt) do (
        accurev diff -b "%%~x"
        IF errorlevel 2 (
            echo VCS problem with this file: %%~x
        ) ELSE (
            set FILE_LIST=!FILE_LIST! %%x
        )
    )
    
        2
  •  0
  •   Eric J.    16 年前

    如果errorlevel construction有一个 奇怪的特征…如果 返回代码等于或高于 超过指定的错误级别。

    参考文献 this link 了解如何处理这个“特征”。

    推荐文章