2017-12-05 3 views
0
@echo off 
SETLOCAL ENABLEDELAYEDEXPANSION 
set freePort= 
set newPort= 

REM Checking the default value in XML file located at the path 
FOR /F tokens^=4^ delims^=^" %%A in (
'type %~dp0\wlp\usr\servers\defaultServer\server.xml ^| find "httpEndpoint host="' 
) do (
    echo: Default Port in XML = %%A 
    set /a startPort=%%A 

) 
echo myCurrent port %startPort% 

:SEARCHPORT 
netstat -o -n -a | find "LISTENING" | find ":%startPort% " > NUL 
if "%ERRORLEVEL%" equ "0" (
    echo "port unavailable %startPort%" 

REM Here I want to ask user to enter a port number rather than hard-coded value ??? 
    set /a startPort=9080 
    echo myNew port %startPort% 
    GOTO :SEARCHPORT 

) ELSE (
    echo "port available %startPort%" 
    set freePort=%startPort% 
    GOTO :FOUNDPORT 
) 

:FOUNDPORT 
echo free %freePort% 

REM here I want to change the value of the httpPort in XML and save the xml file, and then launch the default browser with https:\\localhost:<freePort>MyApp ??? 


) 
@pause 

server.xmlの内容は次のとおりです。Windowsコマンドインタープリタを使用してXMLファイルを処理ユーザーからの価値を受け入れ、XMLファイルの特定の値を変更してブラウザを起動する方法はありますか?

<server description="new server"> 

<!-- 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="27" httpPort="5357" httpsPort="9443" id="defaultHttpEndpoint"/> 

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


<applicationMonitor updateTrigger="mbean"/> 

+0

XMLファイルをネイティブに読み書きできるスクリプト言語を使用することを強くお勧めします。 Powershell、VbscriptまたはJscriptはいくつかのオプションになります。バッチファイルがそれを行うためには、それを行うには無理な力を使います。 – Squashman

+0

ほぼ完了です。バッチスクリプトのxmlファイルにhttpPort = "< >"の新しい値を入れたいだけです。誰も簡単な数行のコードを与えることができます – Aryabhatta

+0

Xidel、cmdlineツールをXML文書の解析に使用してみてください。 http://www.videlibri.de/xidel.html – user2956477

答えて

1

はひどいです。 cmd.exeは、コマンドとアプリケーションを実行するために設計されており、XMLファイルを解析して変更するためのものではありません。バッチファイルで実行するのではなく、C、C++またはC#で実行可能ファイルを記述する方がずっと良いでしょう。

しかし、ここではバッチファイルと同じディレクトリに格納されなければならないXMLファイルserver.xmlに環境変数NewPortに割り当てられた新しいポート番号で属性httpPortの現在のポート番号を交換するためのコメントのバッチファイルです。このコードには属性の番号のようないくつかの制限がありますので、コメントをお読みくださいhttpsPorthttpPortの番号と異なる必要があります。それ以外の場合は2つの番号が同時に置き換えられます。

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

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

rem Define some environment variables which are needed later. 
set "NewPort=47680" 
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 

Perlの正規表現を使用してXMLファイルに値を交換するためのバッチファイル/ JScriptのハイブリッドであるデイブベンハムによって書か例えばJREPL.batために使用されるはるかに良いです。それも、空の文字列可能性があり、属性httpPortの現在の値が何であるかは重要ではありませんjrepl.batを使用することにより

@echo off 
if not exist "%~dp0server.xml" goto :EOF 
set "NewPort=47681" 
call "%~dp0jrepl.bat" "(httpPort=[\x22']?)\d*([\x22']?)" "$1%NewPort%$2" /I /F "%~dp0server.xml" /O - 
rem JREPL.BAT v7.9 leaves behind JREPL\XBYTES.HEX in folder for temporary files. 
rd /Q /S "%TEMP%\JREPL" 2>nul 

。大文字小文字を区別しないPerlの正規表現replaceは、二重引用符ではなく一重引用符で囲まれた値を持つ属性httpPortを処理することさえありますが、XMLファイルでは有効ではありません。

\x22は、検索引数文字列自体が二重引用符で囲まれているため、ここでWindowsコマンドインタープリタの問題を回避するために使用される"の16進表記です。

関連する問題