爲了存儲從一個文本文件中的變量中多行,我現在用的是回答這個問題: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
非常感謝你們的幫助。批量顯然不適合我,但我需要通過強化這一點。
您是否真的需要一個變量中的所有行,或者是否可以更簡單地使用_array_,每個變量一行? – jeb