How to change the file path in my batch script

I have a batch script Change.bat as below  which changes value of attribute httpPort in server.xml file to a hardcoded value. But my script works only if the xml file is in the same folder where the batch script exists. BUT      
The server.xml location is %~dp0\wlp\usr\servers\defaultServer\server.xml and the batch script is in the same folder where 'wlp' folder resides. What changes do I need to do in the batch ?

server.xml contents-
<!-- Enable features -->
<featureManager>
    <feature>webProfile-7.0</feature>
    <!-- <feature>localConnector-1.0</feature> -->
</featureManager>

<!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
<httpEndpoint host="*" httpPort="7680" httpsPort="9443" id="defaultHttpEndpoint"/>

<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>


<applicationMonitor updateTrigger="mbean"/>

Open in new window

Change.bat contents-
@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem First check if the file to modify exists in directory of batch file.
set "XmlFile=%~dp0\wlp\usr\server.xml"
if not exist "%XmlFile%" goto EndBatch

rem Define some environment variables which are needed later.
set "NewPort=47682"
set "LineNumber="
set "LineCount=0"
set "TmpFile=%TEMP%\%~n0.tmp"

rem Search for the line containing attribute httpPort and get its
rem line number and the line itself loaded into environment variables.

for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /L /N /C:httpPort= "%XmlFile%" 2^>nul') do (
    set "LineNumber=%%I"
    set "PortLine=%%J"
)

rem If no line with attribute httpPort found, exit this batch file.
if not defined LineNumber goto EndBatch

rem Determine current number of attribute httpPort independent on where
rem this attribute is specified in the XML line and replace this number
rem in the line with the new port number as defined before.

rem It is required for this type of number replace that the other port
rem number for httpsPort is not the same number as current number for
rem httpPort as in this case both numbers would be replaced by the new
rem number. The attribute name and the equal sign cannot be included in
rem the string substitution as used here.

setlocal EnableDelayedExpansion
set "PortNumber=!PortLine:*httpPort=!"
for /F %%I in ("!PortNumber:~1!") do set "PortNumber=%%~I"
set "PortLine=!PortLine:"%PortNumber%"="%NewPort%"!"
endlocal & set "PortLine=%PortLine%"

rem Make sure the temporary file used next does not already exist.
del "%TmpFile%" 2>nul

rem Copy all lines from XML file to a temporary file including empty
rem lines with the exception of the line containing attribute httpPort
rem which is copied to temporary file with the modified port number.
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /R /N "^" "%XmlFile%" 2^>nul') do (
    set "XmlLine=%%J"
    set /A LineCount+=1
    setlocal EnableDelayedExpansion
    if not !LineCount! == %LineNumber% (
        echo/!XmlLine!>>"%TmpFile%"
    ) else (
        echo/!PortLine!>>"%TmpFile%"
    )
    endlocal
)

rem Overwrite original file with temporary file automatically deleted on success.
move /Y "%TmpFile%" "%XmlFile%" >nul

:EndBatch
endlocal

Open in new window

Aryabhatta MAsked:
Who is Participating?
 
Bill PrewCommented:
The only line you should have to change to the correct location of the XML file to be modified is:

set "XmlFile=%~dp0\wlp\usr\server.xml"


»bp
0
 
Aryabhatta MAuthor Commented:
I changed the location of xml to set "XmlFile=%~dp0\wlp\usr\server.xml" and tested. But then the batch is not updating the httpPort value in the xml file. Am i missing something. Can you please check .
0
 
Bill PrewCommented:
I just tested the script here and it updated the line as follows, is this not correct?

<httpEndpoint host="*" httpPort="47682" httpsPort="9443" id="defaultHttpEndpoint"/>

Open in new window


»bp
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Bill PrewCommented:
In your original question you mentioned the location of the XML as:
The server.xml location is %~dp0\wlp\usr\servers\defaultServer\server.xml
But in your recent comment you stated:
I changed the location of xml to set "XmlFile=%~dp0\wlp\usr\server.xml"
Shouldn't those be the same path?


»bp
0
 
Aryabhatta MAuthor Commented:
I am extremely sorry for the wrong paths. Change.bat and 'wlp' folder  are in same directory, say in drive E:\ .  And server.xml is in folder defaultServer which is much inside wlp folder and path is wlp\usr\servers\defaultServer\server.xml . If both batch and xml are in same folder then the batch works but when xml is in a subfolder then I am not able to make the batch work. Please share your expert comment  what needs to be changed in my batch file.
0
 
Bill PrewCommented:
If the BAT and XML files are in different folders, just set the correct full path to the XML file.

set "XmlFile=E:\wlp\usr\servers\defaultServer\server.xml"

Open in new window


»bp
0
 
Aryabhatta MAuthor Commented:
Changed set "XmlFile=E:\wlp\usr\servers\defaultServer\server.xml". That did not work that's why I posted for expert peoples advise. I am a new student in batch scripting. can you test putting xml in a sub folder and double check.
0
 
Bill PrewCommented:
I did place it in a subfolder, and it worked fine.  I changed the SET in my BAT file to:

set "XmlFile=B:\EE\EE29072271\wlp\usr\servers\defaultServer\server.xml"

Open in new window

And below is the console log from the test, notice it changed the port.

[Wed 12/06/2017 11:59:01.95 B:\EE\EE29072271]tree /a /f
Folder PATH listing for volume bill
Volume serial number is XXXXXXXXXXX
B:.
|   EE29072271.bat
|
\---wlp
    \---usr
        \---servers
            \---defaultServer
                    server.xml


[Wed 12/06/2017 11:59:05.86 B:\EE\EE29072271]type wlp\usr\servers\defaultServer\server.xml
<!-- Enable features -->
<featureManager>
    <feature>webProfile-7.0</feature>
    <!-- <feature>localConnector-1.0</feature> -->
</featureManager>

<!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
<httpEndpoint host="*" httpPort="7680" httpsPort="9443" id="defaultHttpEndpoint"/>

<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>


<applicationMonitor updateTrigger="mbean"/>

[Wed 12/06/2017 11:59:16.64 B:\EE\EE29072271]EE29072271.bat

[Wed 12/06/2017 11:59:19.61 B:\EE\EE29072271]type wlp\usr\servers\defaultServer\server.xml
<!-- Enable features -->
<featureManager>
    <feature>webProfile-7.0</feature>
    <!-- <feature>localConnector-1.0</feature> -->
</featureManager>

<!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
<httpEndpoint host="*" httpPort="47682" httpsPort="9443" id="defaultHttpEndpoint"/>

<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>


<applicationMonitor updateTrigger="mbean"/>

[Wed 12/06/2017 11:59:22.42 B:\EE\EE29072271]

Open in new window


»bp
0
 
Aryabhatta MAuthor Commented:
It works, some / got deleted in my script. Real Thanks to you for giving confidence.
0
 
Bill PrewCommented:
Great, good work, glad you were able to track that down.


»bp
0
 
Aryabhatta MAuthor Commented:
Thanks a lot for giving confidence. Great coach.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.