17

status.xml ファイルを読み取り、status フィールドの値を見つける共通の .bat ファイルがあります。このバッチ ファイルは、ステータス値を見つけるために他のバッチ ファイルによって呼び出されます。呼び出し元のバッチ ファイルは、ファイル名を共通のバッチ ファイルに送信します。共通バッチ ファイルから呼び出しバッチ ファイルにステータスを送信できません。誰か助けてくれませんか?

main batch file
-- will call the common bat file and send the file name and a variable as arguments
setlocal
call Common.bat c:\folderdir\files\status.xml val1
-- trying to print the status returned by the common bat file
echo [%val1%]

common batch file
@ECHO off
setlocal EnableDelayedExpansion

rem will loop through the file and read the value of the status tag
(for /F "delims=" %%a in (%1) do (
set "line=%%a"
set "newLine=!line:<Interface_status>=!"
set "newLine=!newLine:</Interface_status>=!"
if "!newLine!" neq "!line!" (
  @echo Status is !newLine!
rem I want to send`enter code here` the value of newLine to the calling batch file
  set %~2 = !newLine!   <--this does not work
)

)) 
4

2 に答える 2

5

SETLOCAL/ENDLOCAL ブラケット (ここで EOF=ENDLOCAL) 内では、環境に対して行われたすべての変更が取り消されます。

最後の閉じ括弧の後に表示される変数を設定する必要がありCommon.batます(つまり、戻り値-空の文字列になる可能性があります.

次に、common.batの最後の閉じ括弧の後の行に、次の行を入れます。

ENDLOCAL&set %~2=%returnvalue%

ここreturnvalueには、戻りたい値が含まれています(面白い、それ...)

ところで: 文字列SETは SPACE-SENSITIVE です。行が機能していれば、変数を設定していたでしょう"VAR1 "- ではなく"VAR1"- の前のスペース=は変数名に含まれていました - の後のスペース=も同様に割り当てられた値に含まれていました。

構文

set "var=value"

多くの場合、行の末尾の空白を除外するために使用されます (一部の編集者によって残される可能性があるため)。


(はぁ)...

@ECHO off
setlocal EnableDelayedExpansion

rem will loop through the file and read the value of the status tag
(for /F "delims=" %%a in (%1) do (
set "line=%%a"
set "newLine=!line:<Interface_status>=!"
set "newLine=!newLine:</Interface_status>=!"
if "!newLine!" neq "!line!" (
  @echo Status is !newLine!
rem SET THE RETURN VALUE
  set RETURNVALUE=!newLine!
)

)) 

ENDLOCAL&SET %~2=%RETURNVALUE%
于 2013-07-19T16:27:41.550 に答える