1

I have multiple variables containing file paths

set path[1]=\\192.168.0.20\Test1
set path[2]=\\192.168.0.20\Test2
set path[3]=\\192.168.0.20\Test

Now I want to have a loop running that performs some operations with one path at a time. So in theory i want a variable inside a loop, lets call it "currentPath" that changes at each iteration.

Here is my go at it:

setlocal enabledelayedexpansion
for /l %%n in (1,1,%iP%) do (
echo !path[%%n]!
set currentPath=!path[%%n]!
echo variable:%currentPath%
net use %currentPath% /user:%hbmUser% %hbmPW%
) 

The output however is not what I was looking for (for the first iteration):

\\192.168.0.20\Test1
variable:

and "net use" is of course not working since the variable is empty... Since the first echo is working I have no idea why I cant get the currrentPath variable to change to the new value... what am I doing wrong?

l.wach
  • 67
  • 1
  • 1
  • 10
  • 3
    Change `%currentPath%` to `!currentPath!`. – SomethingDark Oct 13 '16 at 11:09
  • 2
    Possible duplicate of [Windows Batch Variables Won't Set](http://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set) – SomethingDark Oct 13 '16 at 11:10
  • awesome thank you so much! – l.wach Oct 13 '16 at 11:17
  • 1
    Odd that you would use delayed expansion in two spots but not the other. What were you confused about? – Squashman Oct 13 '16 at 12:23
  • It is not necessary to first take the value of the _[array element](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)_ in another variable. You may directly write `echo variable:!path[%%n]!` and `net use !path[%%n]! /user:%hbmUser% %hbmPW%`... – Aacini Oct 13 '16 at 17:24
  • yes I got that but I wanted to use the variable because its quite a long script (200lines) and with variables it guets much cleaner.... – l.wach Oct 14 '16 at 07:20

0 Answers0