0

I am struggling with a batch file with array and for loop.

Here is my old post which is working correctly with the help of 'Compo' and now I want to expand it little bit further but I am unable to do so.

there are different IP and for different IP I want to have different destination to copy the file.

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"

I have created one more array for destination folder

Rem Define your destination folder
Set "fol[0]=R1"
Set "fol[1]=R2"
Set "fol[2]=R3"
Set "fol[3]=R4"

my problem is how can I change the Set "destination=D:\Autobackup\" to Set "destination=D:\Autobackup\R1" in loop.

I have tried the following loop inside loop but I don't to have it like this. I want to iterate only once.

@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 

)
)

I also want print the loop value.

Tarun. P
  • 422
  • 7
  • 16
  • You need to better explain what you want it to do! Do you wish for the `XCopy` process to copy `4` times for each `IP`, or do you want each `IP` to be linked somehow to `1` of those `destination` folders, `%fol[i]%`, _and if so which_? – Compo Jul 10 '18 at 13:36
  • what i need is for differnt ip i have differnt destination folder. in this case for ip 192.168.170.239 the destination folder is D:\Autobackup\R1\ . for ip 192.168.170.240 the destination folder is D:\Autobackup\R2\ and so on... in the end of every loop just a echo 1 finished ..2 finisehd and so on... – Tarun. P Jul 10 '18 at 13:49

2 Answers2

1

Do you mean something like this?

@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 
)

See For /? for its usage information, with particular reference to the For /L section.


You could also use the same For /F structure as your original, but link the last octet of the IP Address with the folder names whilst setting the variables:

@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 
)
Compo
  • 36,585
  • 5
  • 27
  • 39
1

Accordingly to your comment each IP is linked to each folder, so IMHO you should not define the two arrays separately, but make clear this relation from the very beginning:

@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 
)

In this way the creation of more IP:Folder pairs is simpler to write and you have not to count them...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • your solution also works .. thanks for useful answer. It will help me in future to perform this kind of soultion. – Tarun. P Jul 11 '18 at 08:38