3

我有以下代码。除了几个问题外,它工作得很好。首先,我的“如果不是空白”检查根本不起作用。其次,在每次迭代期间,它最后一次将服务器名作为服务器名和文件夹传递,我真的不明白......

代码

 SET servers=server01,server02
 SET drive=c
 SET root=program files (x86)\test\
 SET backup=%root%\backup 
 SET folders=folder01\,folder02\file01.txt


 FOR %%b in (%servers%) do (
    FOR %%a in (%folders% %%b) DO (
        if not %%a == "" call :backup %%b %%a
    )
 )


:backup
 SET currentfile="\\%1\%drive%$\%2"
 setlocal
 echo Backing Up %currentfile%
 REM mkdir "\\%1\%drive%$\%backup%"
 REM xcopy /I
 exit /b
 endlocal
 goto :end

输出

1] Backing Up "\\server01\c$\folder01\"
2] Backing Up "\\server01\c$\folder02\file01.txt"
3] Backing Up "\\server01\c$\server01"
4] Backing Up "\\server02\c$\folder01\"
5] Backing Up "\\server02\c$\folder02\file01.txt"
6] Backing Up "\\server02\c$\server02"
7] Backing Up "\\\c$\"

如您所见,不需要第 3、6 和 7 行。

4

1 回答 1

4

第 3 行和第 6 行是由于%%b内部 for 循环引起的。第 7 行是因为脚本在循环后没有终止,所以它在:backup最后再次运行代码。

以下似乎给出了预期的结果:

@echo off

SET servers=server01,server02
SET drive=c
SET root=program files (x86)\test\
SET backup=%root%\backup 
SET folders=folder01\,folder02\file01.txt


FOR %%b in (%servers%) do (
    FOR %%a in (%folders%) DO (
        rem echo a is %%a
        if not %%a == "" call :backup %%b %%a
    )
)

exit /b


:backup
SET currentfile="\\%1\%drive%$\%2"
setlocal
echo Backing Up %currentfile%
REM mkdir "\\%1\%drive%$\%backup%"
REM xcopy /I
endlocal
于 2012-08-03T19:58:55.210 回答