我在处理带有数组和for循环的批处理文件。
这是my old post,它在'Compo'的帮助下可以正常工作,现在我想进一步扩展它,但我无法这样做。
有不同的IP,对于不同的IP,我想有不同的目的地来复制文件。
Rem Define your IP list
Set "obj[0]=192.168.170.239"
Set "obj[1]=192.168.170.240"
Set "obj[2]=192.168.170.241"
Set "obj[3]=192.168.170.242"
我还为目标文件夹创建了一个数组
Rem Define your destination folder
Set "fol[0]=R1"
Set "fol[1]=R2"
Set "fol[2]=R3"
Set "fol[3]=R4"
我的问题是如何在循环中将Set "destination=D:\Autobackup\"
更改为Set "destination=D:\Autobackup\R1"
。
我已经尝试了以下循环循环,但是我不想这样。我只想重复一次。
@Echo Off
Rem Undefine any existing variables beginning with obj[
For /F "Delims==" %%A In ('Set obj[ 2^>Nul') Do Set "%%A="
Rem Define your IP list
Set "obj[0]=192.168.170.239"
Set "obj[1]=192.168.170.240"
Set "obj[2]=192.168.170.241"
Set "obj[3]=192.168.170.242"
Rem Define your destination folder
Set "fol[0]=R1"
Set "fol[1]=R2"
Set "fol[2]=R3"
Set "fol[3]=R4"
Rem Define your Map Source and Destination
Set "map=T:"
Set "source=%map%\Autobackup"
Set "destination=D:\Autobackup\"
Rem Loop through the IP list
For /F "Tokens=1* Delims==" %%A In ('Set obj[ 2^>Nul') Do (
For /F "Tokens=1* Delims==" %%C In ('Set fol[ 2^>Nul') Do (
Rem Make sure that %map% is not currently mapped
Net Use %map% /Delete 2>Nul
Rem Map the share
Net Use %map% \\%%B\D /User:User1 Password
Rem Perform the required operation
XCopy "%source%" "%destination%%%D" /Y
Rem Delete the mapped share
Net Use %map% /Delete
)
)
我也想打印循环值。
答案 0 :(得分:1)
你的意思是这样吗?
@Echo Off
SetLocal EnableDelayedExpansion
Rem Define your IP list
Set "obj[0]=192.168.170.239"
Set "obj[1]=192.168.170.240"
Set "obj[2]=192.168.170.241"
Set "obj[3]=192.168.170.242"
Rem Define your folder list
Set "fol[0]=R1"
Set "fol[1]=R2"
Set "fol[2]=R3"
Set "fol[3]=R4"
Rem Define your Map, Source and Destination
Set "map=T:"
Set "source=%map%\Autobackup"
Set "destination=D:\Autobackup\"
Rem Make sure that %map% is not currently mapped
Net Use %map% /Delete 2>Nul
Rem Loop through each pseudo array items 0-3
For /L %%A In (0,1,3) Do (
Rem Map the share
Net Use %map% \\!obj[%%A]!\D /User:User1 Password
Rem Perform the required operation
XCopy "%source%" "%destination%!fol[%%A]!\" /Y
Rem Delete the mapped share
Net Use %map% /Delete
)
有关使用信息,请参见For /?
,特别参考For /L
部分。
您还可以使用与原始结构相同的For /F
结构,但是在设置变量的同时,将IP地址的最后一个八位位组与文件夹名称链接:
@Echo Off
Rem Define your variable singles
Set "map=T:"
Set "source=%map%\Autobackup"
Set "destination=D:\Autobackup\"
Set "ip3=192.168.170"
Rem Undefine any existing variables beginning with fo[
For /F "Delims==" %%A In ('Set fo[ 2^>Nul') Do Set "%%A="
Rem Define your folder-octet variable pairs
Set "fo[0]=R1.239"
Set "fo[1]=R2.240"
Set "fo[2]=R3.241"
Set "fo[3]=R4.242"
Rem Make sure that %map% is not currently mapped
Net Use %map% /Delete 2>Nul
Rem Loop through each pseudo array item
For /F "Tokens=1* Delims==" %%A In ('Set fo[') Do (
Rem Map the share
Net Use %map% \\%ip3%%%~xB\D /User:User1 Password
Rem Perform the required operation
XCopy "%source%" "%destination%%%~nB\" /Y
Rem Delete the mapped share
Net Use %map% /Delete
)
答案 1 :(得分:1)
根据您的注释,每个IP都链接到每个文件夹,因此恕我直言,您不应分别定义两个数组,而应从一开始就明确此关系:
@echo off
setlocal EnableDelayedExpansion
rem Define the list of IP=FOLDER pairs and use it to define *two* arrays
set "n=0"
for %%a in ("192.168.170.239=R1"
"192.168.170.240=R2"
"192.168.170.241=R3"
"192.168.170.242=R4") do (
for /F "tokens=1,2 delims==" %%x in (%%a) do (
set /A n+=1
set "obj[!n!]=%%x"
set "fol[!n!]=%%y"
)
)
rem Define your Map, Source and Destination
set "map=T:"
set "source=%map%\Autobackup"
set "destination=D:\Autobackup\"
rem Make sure that %map% is not currently mapped
net use %map% /Delete 2>Nul
rem Loop through array elements from 1 to n
for /L %%i in (1,1,%n%) do (
rem Show the loop value
echo Processing %%i- Map ip !obj[%%i]! to folder !fol[%%i]!
rem Map the share
net use %map% \\!obj[%%i]!\D /User:User1 Password
rem Perform the required operation
xcopy "%source%" "%destination%!fol[%%i]!\" /Y
rem Delete the mapped share
net use %map% /Delete
)
这样,创建更多IP:Folder对就更容易编写,而且您不必计算它们...