本帖最后由 CrLf 于 2012-11-16 22:21 编辑
在 endlocal 后保留变量的构思,除了含换行符的变量外应该均可兼容- @echo off&setlocal enabledelayedexpansion
- set a=123
- set b=abc
- set c=@#$
-
- set "getEndlocal=endlocal"
- for %%a in (a b c) do (
- set "getEndlocal=(for /f "delims=" %%$ in (^!%%a^!)do !getEndlocal!&set "$_%%~a=%%$")"
- )
-
- echo 实际命令:
- echo !getEndlocal!
- echo;
-
- %getEndlocal%
-
- echo 执行后的效果:
- set $_
- pause
复制代码 如果写成函数就是这样(格式先随便写啦,仅作示例,回头再配合标准改写):- :getEndlocal varName [varName [varName [...]]] Ver:0.1 By:bathome-Crlf
- setlocal enabledelayedexpansion&set "$=endlocal"
- for %%a in (%*) do set "$=(for %%$ in (^!%%a^!)do !$!&set "$_%%~a=%%$")"
- for /f "delims=" %%a in ("!$!") do endlocal&set "getEndlocal=%%a"
- ::getEndlocal函数结束
复制代码
|