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>