1

I am trying to create a batch file to copy the file over network.

first i am mapping the drive using NETcommand and than copy file using xcopy.

follwing batch file works without any problem

    net use T: \\192.168.170.239\D /user:User1 PASSWROD
    set source=T:\backup
    set destination=D:\backup\
    xcopy %source% %destination% /y
    net use T: /delete 
    TIMEOUT 5

I would like to replace the static IP '192.168.170.239' and make any array of ip as below and replace the netuse command in a loop.

@echo off 
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 treid the following but didn't work

 @echo off
set len=2
set obj[0]=192.168.170.239
set obj[1]=192.168.170.240


set i=0
:loop
if %i% equ %len% goto :eof
for /f "usebackq delims== tokens=2" %%j in (`set obj[%i%]`) do (

net use T: \\%%j\D /user:User1 Password
TIMEOUT 10
set source=T:\Autobackup
set destination=D:\Autobackup\
xcopy %source% %destination% /y
net use T: /delete 
TIMEOUT 10

)
set /a i=%i%+1
goto loop

It works for the second ip but not for the first ip.

Tarun. P
  • 422
  • 7
  • 16
  • No error messages? Also, what's up with `set source=T:\backup` and `set source=T:\Autobackup` in your examples? – notjustme Jul 09 '18 at 13:05
  • There is no need to set a variable `len` that defines the number of (pseudo-)array elements, since `set obj[` returns all elements, which you could count or loop through... – aschipfl Jul 09 '18 at 14:21
  • 2
    I suggest you to read [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) about array management in Batch files... – Aacini Jul 09 '18 at 14:41
  • if i want to create another arrary, for all differnt ip the destination folder is different how can i change the `Set "destination=D:\Autobackup\"` to `Set "destination=D:\Autobackup\Folder[0]"` – Tarun. P Jul 10 '18 at 10:51

2 Answers2

1

You should really be looking at a structure more like this:

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

    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%" /Y

    Rem Delete the mapped share
    Net Use %map% /Delete 
)
Compo
  • 36,585
  • 5
  • 27
  • 39
  • I have tried but it didn't map the drive. i have put echo command after net use command `echo %%A ` it returns `obj[0]` not the ip address – Tarun. P Jul 09 '18 at 13:52
  • it works without any problem. I am glad if you can explain a **two foor loop** with ` %%A and %%B ` – Tarun. P Jul 09 '18 at 14:04
  • @Tarun.P, I don't see two for loops, There is only one. `%%A` and `%%B` tokens belong to the same loop, delimited by equal sign(`=`). `set obj[` returns list of variables which begins with `obj[`. ex: in `obj[3]=192.168.170.241` the first token `%%A` is `obj[3]` and the second token `%%B` is `192.168.170.241` – sst Jul 10 '18 at 12:01
  • thanks for the info but i don't know why i am unable to understand this loop. Anyway can you help me with this...if i want to create another arrary, for all differnt ip the destination folder is different how can i change the Set "destination=D:\Autobackup\" to Set "destination=D:\Autobackup\Folder[0]" – Tarun. P Jul 10 '18 at 12:44
  • @Tarun.P, if you have a new question, then please post it as such. An answer here isn't an open invitation to lifetime support for any and all future modifications or requirement changes. – Compo Jul 10 '18 at 13:30
  • @Compo I have created a new post. [link] (https://stackoverflow.com/questions/51266265/create-two-foor-loop-for-two-differnt-array-in-batch-file) – Tarun. P Jul 10 '18 at 13:35
1

Compo's answer is good. Another way to construct the loop follows. It does not use an "array" of variables. I find this easier to edit and understand.

SET IPLIST=^
192.168.170.239 ^
192.168.170.240 ^
192.168.170.241 ^
192.168.170.242

FOR %%a IN (%IPLIST%) DO (
    ECHO %%a
)
lit
  • 14,456
  • 10
  • 65
  • 119