我用数组和循环批处理文件中挣扎。
这里是我的老后 ,其与“康宝”的帮助下正常工作,现在我想扩大它远一点,但我不能这样做。
有不同的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
)
)
我也想打印循环值。
你的意思是这样的?
@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
)
因此,以您的评论每个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创建:文件夹对简单写,你有没有算他们...