I am trying to find and replace string with another string in a file.
I have an XML file where port=
is present at multiple locations. Every port has different value like port="443"
and port="8011"
.
I want to find a particular port
and its value. The current value of port
is not known on batch execution as the value varies. This variable and on batch execution unknown value of one specific port
attribute in the XML file should be replaced with a new value I am passing as a parameter to the batch file.
How can I find the particular port
value from a file and replace it with a new value?
I have used the below code for replace string, but I am not able to change its value.
if EXIST %ConfPath% (echo "here") ELSE (
for /f "tokens=1,* delims=¶" %%A in ( '"type %file%"' ) do (
SET string=%%A
SET modified=!string:%oldport%=%newport%!
echo !modified!
)
)
Edit:
Here is my XML file:
<?xml version='1.0' encoding='utf-8'?>
<Server port="first" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="second" redirectPort="forth" />
<Connector port="third" protocol="HTTP/1.1" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" URIEncoding="UTF-8" keystoreFile="myKey" keystorePass="myPass" />
</Service>
</Server>
There are four ports present in above file.
I want to change port="third"
with port="newPort"
.
Until now I have replaced the string, but I'm not able to concatenate it with the remaining file.
First I get the line number and after that I split with quotes and replace. But I I'm not able to concatenate it. Something is missing but I cannot find it.
My batch code is:
for /f "tokens=1,* delims=" %%A in ( '"type %file%"' ) do (
SET /a line+=1
IF !line!==11 (
SET string=%%A
for /f tokens^=2^ delims^=^" %%I in ( "!string!" ) do (set repPort=%%I)
SET modified=!string:repPort=PORT!
)
echo !modified!
)
Is this the right approach and how can I concatenate?