テキスト ファイルの変数内に複数の行を格納するために、この質問の回答を使用しています: https://stackoverflow.com/a/10624240/1513458
このスクリプトをインラインで使用して大きなファイルを解析すると、最大の setlocal エラーが発生しました。これに対抗するために、スクリプトをサブルーチンに入れ、必要な行から呼び出しました。
サブルーチンが終了する前にサブルーチンの下部にあるエコーは、期待される結果を正常に出力します...しかし、最初に呼び出された場所の上部に戻ると、変数は空です。何故ですか?
スクリプトの短縮版は次のとおりです。
setlocal EnableDelayedExpansion
set sourcefile=cc1
call :readfile %sourcefile%
echo !insert!
goto :EOF
:readfile
SETLOCAL DisableDelayedExpansion
set "insert="
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ %SOURCELOC%\%1.txt"`) do (
set "line=%%a"
SETLOCAL EnableDelayedExpansion
set "line=!line:#=#S!"
set "line=!line:*:=!"
for /F "delims=" %%p in ("!insert!#L!line!") do (
ENDLOCAL
set "insert=%%p"
)
)
SETLOCAL EnableDelayedExpansion
if defined insert (
set "insert=!insert:~2!"
set ^"insert=!insert:#L=^
!"
set "insert=!insert:#S=#!"
)
rem A echo !insert! here would output the expected result
goto :EOF
ご協力ありがとうございました。バッチは明らかに私向けではありませんが、これをやり抜く必要があります。