DevSupport
asked on
Finding the port number and storing it in a variable
Using batch script in windows, I am trying to grep the port number (connector port value) from a file (abc.xml) which has the following data:
Define a non-SSL HTTP/1.1 Connector on port 6080
<Connector port="6080" protocol="HTTP/1.1"
port="6080" protocol="HTTP/1.1"
<!-- Define a SSL HTTP/1.1 Connector on port 6043
<Connector port="6043" protocol="HTTP/1.1" SSLEnabled="true"
I'd like to store the value (6080) into a variable named port.
Please help me with a batch script. I tried using findstr /i to find the value but the I am unable to get the value.
Any help would be appreciated.
Thanks
Amrith
Define a non-SSL HTTP/1.1 Connector on port 6080
<Connector port="6080" protocol="HTTP/1.1"
port="6080" protocol="HTTP/1.1"
<!-- Define a SSL HTTP/1.1 Connector on port 6043
<Connector port="6043" protocol="HTTP/1.1" SSLEnabled="true"
I'd like to store the value (6080) into a variable named port.
Please help me with a batch script. I tried using findstr /i to find the value but the I am unable to get the value.
Any help would be appreciated.
Thanks
Amrith
Note that what you posted is not valid XML.
Due to the format of XML, this is impossible to do reliably in batch.
The following should work for the example you posted, but it will miserably fail if the XML is, for example, saved as one long line.
Due to the format of XML, this is impossible to do reliably in batch.
The following should work for the example you posted, but it will miserably fail if the XML is, for example, saved as one long line.
@echo off
setlocal enabledelayedexpansion
set XmlFile=C:\Temp\test.xml
for /f "tokens=1-10 delims=<>" %%a in ('type "%XmlFile%" ^| findstr.exe /i /v /r /c:"SSLEnabled=.true" ^| findstr.exe /i /r /c:"protocol=.HTTP"') do (
set Line=%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j
set Next=
for %%t in (!Line!) do (
if /i "%%t"=="port" (
set Next=True
) else (
if defined next set Port=%%~t
set Next=
)
)
)
echo Port: %Port%
What OS are you running this on? This could be done with a few lines of Powershell 2.0 or later (integrated into a batch script, if required). If you can use PS, please attach the complete (sanitized!) XML file you're using (as Style Code or Code Snippet please, not as comment text!).
ASKER
Thank You oBdA for the immediate response, great! It works for me.
I was using findstr and I could filter out the data which I had provided, from the master xml file which I thought of filtering further.
It even works for the master xml file.
Please find attached the complete XML and Thanks a lot!
server.xml
I was using findstr and I could filter out the data which I had provided, from the master xml file which I thought of filtering further.
It even works for the master xml file.
Please find attached the complete XML and Thanks a lot!
server.xml
ASKER
I'd like to know how to get the value "6043" value from the string "Connector port="6043" " from the xml file, Sorry I would have asked you earlier, Kindly let me know. Thank You!
Based on the first batch:
As far as PS is concerned: what's your OS and PS version (open a PS and enter "$Host-Version")?
Do you need both ports or just the SSL?
@echo off
setlocal enabledelayedexpansion
set XmlFile=C:\Temp\test.xml
for /f "tokens=1-10 delims=<>" %%a in ('type "%XmlFile%" ^| findstr.exe /i /r /c:"protocol=.HTTP/1\.1. SSLEnabled=.true"') do (
set Line=%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j
set Next=
for %%t in (!Line!) do (
if /i "%%t"=="port" (
set Next=True
) else (
if defined next set SSLPort=%%~t
set Next=
)
)
)
echo SSL Port: %SSLPort%
As far as PS is concerned: what's your OS and PS version (open a PS and enter "$Host-Version")?
Do you need both ports or just the SSL?
From what I can see, the XML you posted only has one HTTP listener, and this one is without SSL, and is listening on Port 6080.
The one listening on 6043 is using SSL,, but uses Nio, unlike your example above.
Which one(s) do you need?
The one listening on 6043 is using SSL,, but uses Nio, unlike your example above.
Which one(s) do you need?
ASKER
Hi oBdA,
I need the SSL one listening to Nio, (6043)
Thank You!
Regards
Amrith
I need the SSL one listening to Nio, (6043)
Thank You!
Regards
Amrith
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You so much, really helpful!!
Open in new window
The %%~F removes the double quotes, if contained.