代码之家  ›  专栏  ›  技术社区  ›  Tarun. P

为批处理文件中的两个不同数组创建两个for循环

  •  0
  • Tarun. P  · 技术社区  · 7 年前

    我正在用数组和for循环处理批处理文件。

    这是 my old post 在…的帮助下工作正常 '公司' 现在我想把它再扩展一点,但我做不到。

    有不同的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 
    
    )
    )
    

    我还想打印循环值。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Compo    7 年前

    你的意思是这样的吗?

    @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 
    )
    
        2
  •  1
  •   Aacini    7 年前

    根据你的 评论 每个IP都链接到 每个 文件夹,所以imho您不应该单独定义这两个数组,而是从一开始就明确这一关系:

    @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对就更简单了,您不必计算它们…