为了存储从一个文本文件中的变量中多行,我现在用的是回答这个问题: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