问题
I want to write a code in batch like the following:
for %%a in (%list%) do (
command1
command2
command3
command4
.
.
.
)
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?
complete script:
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
)
)
Thanks
回答1:
It's clear now where the error comes from.GOTO
breaks the FOR
context and %%a
does not exist anymore . Rewrite the batch using subroutines and CALL
instead of GOTO
来源:https://stackoverflow.com/questions/26013750/second-command-in-a-batch-for-loop