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.