Loops For/f批处理循环失败,错误为“这在当时是出人意料的。”;

Loops For/f批处理循环失败,错误为“这在当时是出人意料的。”;,loops,batch-file,for-loop,Loops,Batch File,For Loop,我正在写一个简单的批处理,它将通过一个“FOR/F”循环通过一个.txt输入文件,检查一些PC的3个文件位置,这将决定操作系统或应用程序的版本,设置几个变量,然后重命名一个文件并在其位置复制一个新文件..但我无法让它工作。 批处理失败,此时出现意外情况 如蒙协助,将不胜感激。 这是批量内容 CD /D "C:\Win32app\Scripts\SEPXML" FOR /f "Tokens=1" %%a IN (List.txt) DO ( Echo %%a If exist "\

我正在写一个简单的批处理,它将通过一个“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
)

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

每次在
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
    )
)

感谢您的回复。很抱歉,我必须问一个可能很愚蠢的问题,但%%b变量来自哪里。我已经测试了您的上述代码,但收到一个错误“%b\sylink.xml”此时是意外的。C:\Win32app\Scripts\SEPXML>)如果未定义,则执行如果存在“%b\sylink.xml”,则执行(@mark,对不起,是我的错。
%%b
被分配到内部for循环中,而
%%a
连接到路径。发生错误的原因是我键入了
if exists
,而不是
if exists
。现在
s
在code>中被删除。@mark,并且有一个传统错误。
%b
正在lis>上迭代对于带引号的字符串,所有引用都应该是
%%~b
,以删除引号。也在代码中更正谢谢。我必须承认,我不完全理解此代码是如何运行的,因此你的脚本能力让我感到羞愧。我必须说,我对它的效果印象非常深刻。再次感谢你。我很感谢你接受了我的建议是时候帮助我了。