【问题标题】:For /f batch loop fails with error ") was unexpected at this time."For /f 批处理循环失败并出现错误“)此时是意外的。”
【发布时间】:2014-09-04 14:26:17
【问题描述】:

我正在编写我认为是一个简单的批处理,它将通过“FOR /F”循环通过一个 .txt 输入文件,以检查多个 PC 的 3 个文件位置,这将确定操作系统或应用程序版本,设置几个变量然后重命名一个文件并在它的位置复制一个新文件..但我无法让它工作。 批处理失败,此时 ) 出乎意料

任何帮助将不胜感激。 这是批处理内容

CD /D "C:\Win32app\Scripts\SEPXML"
FOR /f "Tokens=1" %%a IN (List.txt) DO (
    Echo %%a
    If exist "\\%%a\c$\Documents and Settings\All Users\Application Data\Symantec\Symantec Endpoint Protection\CurrentVersion\Data\Config\SyLink.xml" (GoTo :Win2k3) else (GoTo :Next1)
    :Next1
    If exist "\\%%a\c$\Win32app\Symantec\SEPP\SyLink.xml" (GoTo :x86Serv) else (GoTo :Next2)
    :Next2
    If exist "\\%%a\c$\ProgramData\Symantec\Symantec Endpoint Protection\CurrentVersion\Data\Config\sylink.xml" (GoTo :Win2K8) else (GoTo :WriteError)

    :Win2k3
    Set SyLinkPath="\\%%a\c$\Documents and Settings\All Users\Application Data\Symantec\Symantec Endpoint Protection\CurrentVersion\Data\Config"
    Set OSVer=Win2k3
    GoTo :Run

    :x86Serv
    Set SyLinkPath="\\%%a\c$\Win32app\Symantec\SEPP"
    Set OSVer=Win2k3OLD
    GoTo :Run

    :Win2K8
    Set SyLinkPath="\\%%a\c$\ProgramData\Symantec\Symantec Endpoint Protection\CurrentVersion\Data\Config"
    Set OSVer=Win2k8
    GoTo Run

    :Run
    Ren %SyLinkPath%\SyLink.xml %SyLinkPath%\SyLink.old
    Robocopy C:\Win32app\Scripts\SEPXML\SyLink %SyLinkPath% /r:0 /W:0 /copyall /Tee /Log+:C:\Win32app\Scripts\SEPXML\Log\Results.txt
    GoTo End

    :WriteError 
    Echo %%a >>C:\Win32app\Scripts\SEPXML\Log\Error-Servers.txt
    :End
)

【问题讨论】:

    标签: loops batch-file for-loop


    【解决方案1】:

    并非所有内容都需要用括号括起来,但还有一个更大的问题。

    每次在for 循环内执行goto 时,都会取消所有循环。不能在代码块内跳转。

    您可以将处理移动到标签以调用或重构代码以避免跳转操作

    CD /D "C:\Win32app\Scripts\SEPXML"
    
    for /f "tokens=1" %%a in (List.txt) do (
        set "done="
        for %%b in (
            "\\%%a\c$\Documents and Settings\All Users\Application Data\Symantec\Symantec Endpoint Protection\CurrentVersion\Data\Config"
            "\\%%a\c$\Win32app\Symantec\SEPP"
            "\\%%a\c$\ProgramData\Symantec\Symantec Endpoint Protection\CurrentVersion\Data\Config"
        ) do if not defined done if exist "%%~b\sylink.xml" (
            set "done=1"
            Ren "%%~b\SyLink.xml" "SyLink.old"
            Robocopy C:\Win32app\Scripts\SEP-XMLChange\SyLink "%%~b" /r:0 /W:0 /copyall /Tee /Log+:C:\Win32app\Scripts\SEP-XMLChange\Log\Results.txt
        )
        if not defined done (
            Echo %%a >>C:\Win32app\Scripts\SEP-XMLChange\Log\Error-Servers.txt
        )
    )
    

    【讨论】:

    • 感谢您的回复 MC ND。很抱歉,我不得不问一个可能很愚蠢的问题,但是 %%b 变量来自哪里。我已经测试了您的上述代码,但此时收到错误“%b\sylink.xml”是意外的。 C:\Win32app\Scripts\SEPXML> ) 如果没有定义就做 如果存在就做 "%b\sylink.xml " (
    • @mark,对不起,我的错。 %%b 在内部 for 循环中分配,%%a 连接到路径。发生错误是因为我输入了if exists 而不是if exist。现在 s 在代码中被删除了。
    • @mark,还有一个附加错误。由于 %%b 正在遍历引用字符串列表,所有引用都应该是 %%~b 以删除引号。也在代码中更正
    • 谢谢。我必须承认我并不完全理解这段代码是如何运作的,因此你的脚本能力让我感到羞耻。我必须说我印象深刻的是它是一种享受。再次感谢你 MC ND。非常感谢您抽出时间来帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多