批处理中的第二个命令 [英] second command in a batch for loop

查看:79
本文介绍了批处理中的第二个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想批量编写如下代码:

I want to write a code in batch like the following:

for %%a in (%list%) do (
    command1
    command2
    command3
    command4
    .
    .  
    .
)

列表的第一个元素的第一个命令已正确执行.但是在command2中有错误.在cmd窗口中,我可以看到发生了什么.并且在命令中未将%% a更改为该值.有一个%a"而不是值.在我的代码中,完全像command1一样有"%% a".

The first command for the first element of the list is executed correctly. But in command2 there are errors. In the cmd-window I can see what happend. And in the command isn't changed the %%a to the value. There is a "%a" instead of the value. In my code there is "%%a" exactly like in command1.

这是什么错误?如何在批处理循环中执行多个命令?

What is the mistake here? How can I execute more than one command in a batch-for-loop?

完整脚本:

set machines=pc1 pc2 pc3 pc4 
set source1="\\%%a\c$\Data1"
set source2="\\%%a\c$\Data2"
set dest1="\\server\share\%%a\data1\%date%
set dest2="\\server\share\%%a\data2\%date%
set log1=\\server\share\log\log_%date%_data1.txt
set log2=\\server\share\log\log_%date%_data2.txt
set mail="success"
set errormail="error"
set betreff="successmail"
set errorbetreff="errormail"
set empf="dummy@test.com" 
set blat="pathtoblat\blat.exe"
set error=0

for %%a in (%machines%) do (

    %windir%\system32\robocopy.exe %source1% %dest1% /e /COPYALL /log:%log1%

    if %ERRORLEVEL% EQU 16 echo ***FATAL ERROR*** & goto error
    ... different errorlevels
    if %ERRORLEVEL% EQU 0 echo No Change & goto next



    :error
    set error=1



    :next
    %windir%\system32\robocopy.exe %source2% %dest2% /e /COPYALL /log:%log2%

    if %ERRORLEVEL% EQU 16 echo ***FATAL ERROR*** & goto errormail
    ... different errorlevels
    if %ERRORLEVEL% EQU 0 echo No Change



    if error==1 goto errormail
    goto mail



    :errormail
    %blat% -to %empf% -f %COMPUTERNAME%@test.com -server 172.y.y.y -s %errorbetreff% -body %errormail% -attacht %log1%,%log2%
    goto loeschen

    :mail
    %blat% -to %empf% -f %COMPUTERNAME%@test.com -server 172.y.y.y -s %betreff% -body %mail%

    :loeschen
    setlocal enableextensions disabledelayedexpansion
    pushd "\\server\share\backup\%COMPUTERNAME%\data1" && (
        for /f "skip=2 delims=" %%a in (
            'dir /b /ad /tc /o-d'
        ) do rmdir /s /q "%%~fa"
        popd
    )

    setlocal enableextensions disabledelayedexpansion
    pushd "\\server\share\backup\%COMPUTERNAME%\data2" && (
        for /f "skip=2 delims=" %%a in (
            'dir /b /ad /tc /o-d'
        ) do rmdir /s /q "%%~fa"
        popd
    )

    setlocal enableextensions disabledelayedexpansion
    pushd "\\server\share\log" && (
        for /f "skip=4 delims=" %%a in (
            'dir /b /a-d /tc /o-d'
        ) do del /q "%%~fa"
        popd
    )
)

谢谢

推荐答案

现在很清楚错误的来源.GOTO破坏了FOR上下文,并且%%a不再存在.使用子例程和CALL而不是GOTO

It's clear now where the error comes from.GOTO breaks the FOR context and %%adoes not exist anymore . Rewrite the batch using subroutines and CALL instead of GOTO

这篇关于批处理中的第二个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆