0

Hi I'm programming a batch script, and using a for loop to get every node, , in a xml file. Then I want to put them in a array, and I think I'm not doing it correctly.

Here is my code:

FOR /F "delims=: tokens=2" %%i IN ('find /c conf.xml "<ip>" /i') DO echo %%i
setlocal enableDelayedExpansion
set /a n=0
for /F "tokens=3 delims=<>" %%i in ('findstr "<ip>" test.xml')  do (
    set /A n+=1
    echo !n!
    set tabIP[%%n%%] = %%i
    echo !n!
    echo %%i
)
@echo off
for /L %%n in (1,1,5) do echo "%tabIP[%%n]%"
Endlocal

The first loop is to read the xml file, and file my array, and the second loop, to print the content of my array.

When I run my script in my prompt, I have this output:

C:\Users\>test.cmd

C:\Users\test>FOR /F "delims=: tokens=2" %i IN ('find /c conf.xml "<ip>" /i') DO echo %i
Fichier introuvable - CONF.XML

C:\Users\>setlocal enableDelayedExpansion

C:\Users\>set /a n=0

C:\Users\>for /F "tokens=3 delims=<>" %i in ('findstr "<ip>" test.xml') do (
 set /A n+=1
 echo !n!
 set tabIP[%n%] = %i
 echo !n!
 echo %i
)

C:\Users\>(
 set /A n+=1
 echo !n!
 set tabIP[%n%] = ip1
 echo !n!
 echo ip1
)
1
1
ip1

C:\Users\>(
 set /A n+=1
 echo !n!
 set tabIP[%n%] = ip2
 echo !n!
 echo ip2
)
2
2
ip2

C:\Users\>(
 set /A n+=1
 echo !n!
 set tabIP[%n%] = ip3
 echo !n!
 echo ip3
)
3
3
ip3

C:\Users\>(
 set /A n+=1
 echo !n!
 set tabIP[%n%] = ip4
 echo !n!
 echo ip4
)
4
4
ip4

C:\Users\>(
 set /A n+=1
 echo !n!
 set tabIP[%n%] = ip5
 echo !n!
 echo ip5
)
5
5
ip5
""
""
""
""
""

You can see here, at the end, when I try to print my array it prints nothing. I don't know if I'm trying to print it the wrong way, or if I'm not setting its value properly.

Here is my xml file, if needed.

<ip-list>
            <ip>ip1</ip>
            <ip>ip2</ip>
            <ip>ip3</ip>
            <ip>ip4</ip>
            <ip>ip5</ip>
</ip-list>
Compo
  • 36,585
  • 5
  • 27
  • 39
Nono
  • 25
  • 7
  • 2
    Don't use spaces with the `SET` command. You also need to use delayed expansion as well. `set "tabIP[!n!]=%%i"`. Use surrounding quotes as a best practice as well. The iteration of the array should be used like this: `echo "!tabIP[%%n]!"` – Squashman Feb 09 '21 at 19:26
  • Oh thanks a lot ! that worked perfectly! – Nono Feb 09 '21 at 19:51
  • I already saw this , but i wasn't able to see the error in my code , you can say i wasn't able to apply what i read in this post for my own script , but now everything is fine thanks and i'll keep going now ^^ – Nono Feb 10 '21 at 22:24
  • Hi Nono, it is just a way for us to mark questions as duplicates so that people don't have to keep regurgitating the same answers for similar questions. Once three people vote that your question is a duplicate it will be closed and people will be redirected to that question that has multiple answers. – Squashman Feb 10 '21 at 22:31
  • Oh i didn't know , excuse me for that , you can mark it as duplicate it's fine with me – Nono Feb 12 '21 at 09:28

0 Answers0