Link to home
Start Free TrialLog in
Avatar of DevSupport
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
Avatar of Qlemo
Qlemo
Flag of Germany image

We need to be more specific as the port occures more than once.
for /F "tokens=3 delims== " %%F in ('findstr /c:"Connector port=" ^| findstr /v SSL') do set port=%%~F

Open in new window

The %%~F removes the double quotes, if contained.
Avatar of oBdA
oBdA

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.
@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%

Open in new window

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!).
Avatar of DevSupport

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'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:
@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%

Open in new window


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?
Hi oBdA,

I need the SSL one listening to Nio, (6043)

Thank You!

Regards
Amrith
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You so much, really helpful!!