gpt4 book ai didi

batch-file - BAT 文件(批处理)中的嵌套循环返回不需要的结果

转载 作者:行者123 更新时间:2023-12-03 23:35:13 27 4
gpt4 key购买 nike

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

代码

 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 行。

最佳答案

第3、6行是因为内层for循环的%%b引起的。第 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

关于batch-file - BAT 文件(批处理)中的嵌套循环返回不需要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com