3 回答
TA贡献1890条经验 获得超9个赞
echo hello | set /p var= echo here | call :function
echo one | ( set /p varX= & set varX ) set var1=var2 set var2=content of two echo one | ( echo %%%var1%%% ) echo three | echo MYCMDLINE %%cmdcmdline%% echo four | (cmd /v:on /c echo 4: !var2!)
编辑:深度分析
.。百分比膨胀
.。特殊字符相位/块开始检测
.。延迟扩展(但只在启用延迟扩展且不是命令块的情况下)
C:\Windows\system32\cmd.exe /S /D /c"<BATCH COMMAND>"
.。百分比膨胀
.。延迟扩展(但前提是启用延迟扩展)
<BATCH COMMAND>
( echo one %%cmdcmdline%% echo two ) | more
C:\Windows\system32\cmd.exe /S /D /c" ( echo one %cmdcmdline% & echo two )"
&
( set var=one echo !var! set var=two ) | more
!var!
echo !var! | more
编辑:添加LF技巧
&
.
( echo 7: part1 rem This kills the entire block because the closing ) is remarked! echo part2 ) | more
C:\Windows\system32\cmd.exe /S /D /c" ( echo 7: part1 & rem This ...& echo part2 ) "
rem
set LF=^ REM The two empty lines above are required ( echo 8: part1 rem This works as it splits the commands %%LF%% echo part2 ) | more
C:\Windows\system32\cmd.exe /S /D /c" ( echo 8: part1 %cmdcmdline% & rem This works as it splits the commands %LF% echo part2 )"
( echo 8: part1 & rem This works as it splits the commands echo part2 )
%LF%
<linefeed>
编辑:异步不是全部事实
@echo off ( ( for /L %%a in ( 1,1,60 ) DO ( echo A long text can lock this thread echo Thread1 ##### %%a > con ) ) echo Thread1 ##### end > con ) | ( for /L %%n in ( 1,1,6) DO @( ping -n 2 localhost > nul echo Thread2 ..... %%n set /p x= ) )
TA贡献1911条经验 获得超7个赞
@echo off cls setlocal disableDelayedExpansion set var1=value1 set "var2=" setlocal enableDelayedExpansion echo on @echo NO PIPE - delayed expansion is ON echo 1: %var1%, %var2%, !var1!, !var2! (echo 2: %var1%, %var2%, !var1!, !var2!) @echo( @echo PIPE LEFT SIDE - Delayed expansion is ON echo 1L: %%var1%%, %%var2%%, !var1!, !var2! | more (echo 2L: %%var1%%, %%var2%%, !var1!, !var2!) | more (setlocal enableDelayedExpansion & echo 3L: %%var1%%, %%var2%%, !var1!, !var2!) | more (cmd /v:on /c echo 4L: %%var1%%, %%var2%%, !var1!, !var2!) | more cmd /v:on /c echo 5L: %%var1%%, %%var2%%, !var1!, !var2! | more @endlocal @echo( @echo Delayed expansion is now OFF (cmd /v:on /c echo 6L: %%var1%%, %%var2%%, !var1!, !var2!) | more cmd /v:on /c echo 7L: %%var1%%, %%var2%%, !var1!, !var2! | more @setlocal enableDelayedExpansion @echo( @echo PIPE RIGHT SIDE - delayed expansion is ON echo junk | echo 1R: %%var1%%, %%var2%%, !var1!, !var2! echo junk | (echo 2R: %%var1%%, %%var2%%, !var1!, !var2!) echo junk | (setlocal enableDelayedExpansion & echo 3R: %%var1%%, %%var2%%, !var1!, !var2!) echo junk | (cmd /v:on /c echo 4R: %%var1%%, %%var2%%, !var1!, !var2!) echo junk | cmd /v:on /c echo 5R: %%var1%%, %%var2%%, !var1!, !var2! @endlocal @echo( @echo Delayed expansion is now OFF echo junk | (cmd /v:on /c echo 6R: %%var1%%, %%var2%%, !var1!, !var2!) echo junk | cmd /v:on /c echo 7R: %%var1%%, %%var2%%, !var1!, !var2!
NO PIPE - delayed expansion is ON C:\test>echo 1: value1, , !var1!, !var2! 1: value1, , value1, C:\test>(echo 2: value1, , !var1!, !var2! ) 2: value1, , value1, PIPE LEFT SIDE - Delayed expansion is ON C:\test>echo 1L: %var1%, %var2%, !var1!, !var2! | more 1L: value1, %var2%, value1, C:\test>(echo 2L: %var1%, %var2%, !var1!, !var2! ) | more 2L: value1, %var2%, !var1!, !var2! C:\test>(setlocal enableDelayedExpansion & echo 3L: %var1%, %var2%, !var1!, !var2! ) | more 3L: value1, %var2%, !var1!, !var2! C:\test>(cmd /v:on /c echo 4L: %var1%, %var2%, !var1!, !var2! ) | more 4L: value1, %var2%, value1, !var2! C:\test>cmd /v:on /c echo 5L: %var1%, %var2%, !var1!, !var2! | more 5L: value1, %var2%, value1, Delayed expansion is now OFF C:\test>(cmd /v:on /c echo 6L: %var1%, %var2%, !var1!, !var2! ) | more 6L: value1, %var2%, value1, !var2! C:\test>cmd /v:on /c echo 7L: %var1%, %var2%, !var1!, !var2! | more 7L: value1, %var2%, value1, !var2! PIPE RIGHT SIDE - delayed expansion is ON C:\test>echo junk | echo 1R: %var1%, %var2%, !var1!, !var2! 1R: value1, %var2%, value1, C:\test>echo junk | (echo 2R: %var1%, %var2%, !var1!, !var2! ) 2R: value1, %var2%, !var1!, !var2! C:\test>echo junk | (setlocal enableDelayedExpansion & echo 3R: %var1%, %var2%, !var1!, !var2! ) 3R: value1, %var2%, !var1!, !var2! C:\test>echo junk | (cmd /v:on /c echo 4R: %var1%, %var2%, !var1!, !var2! ) 4R: value1, %var2%, value1, !var2! C:\test>echo junk | cmd /v:on /c echo 5R: %var1%, %var2%, !var1!, !var2! 5R: value1, %var2%, value1, Delayed expansion is now OFF C:\test>echo junk | (cmd /v:on /c echo 6R: %var1%, %var2%, !var1!, !var2! ) 6R: value1, %var2%, value1, !var2! C:\test>echo junk | cmd /v:on /c echo 7R: %var1%, %var2%, !var1!, !var2! 7R: value1, %var2%, value1, !var2!
测试1L,1R:
测试2L,2R:括号禁用延迟扩展!
测试3L,3R: setlocal EnableDelayedExpansion
setlocal
测试4L,4R:CMD /V:ON
测试5L,5R:CMD /V:on
测试6L,6R,7L,7R:
增编:
@echo on call echo batch context %%%% call echo cmd line context %%%% | more
C:\test>call echo batch context %% batch context % C:\test>call echo cmd line context %% | more cmd line context %%
最后增编
@echo off cls setlocal disableDelayedExpansion set var1=value1 set "var2=" setlocal enableDelayedExpansion echo on @echo( @echo Delayed expansion is ON echo 1: %%, %%var1%%, %%var2%%, !var1!, ^^^!var1^^^!, !var2!, ^^^!var2^^^!, %%cmdcmdline%% | more (echo 2: %%, %%var1%%, %%var2%%, !var1!, ^^^!var1^^^! !var2!, %%cmdcmdline%%) | more for %%a in (Z) do (echo 3: %%a %%, %%var1%%, %%var2%%, !var1!, ^^^!var1^^^! !var2!, %%cmdcmdline%%) | more ( echo 4: part1 set "var2=var2Value set var2 echo " set var2 ) ( echo 5: part1 set "var2=var2Value set var2 echo " set var2 echo --- begin cmdcmdline --- echo %%cmdcmdline%% echo --- end cmdcmdline --- ) | more ( echo 6: part1 rem Only this line remarked echo part2 ) ( echo 7: part1 rem This kills the entire block because the closing ) is remarked! echo part2 ) | more
Delayed expansion is ON C:\test>echo 1: %, %var1%, %var2%, !var1!, ^!var1^!, !var2!, ^!var2^!, %cmdcmdline% | more 1: %, value1, %var2%, value1, !var1!, , !var2!, C:\Windows\system32\cmd.exe /S /D /c" echo 1: %, %var1%, %var2%, value1, !var1!, , !var2!, %cmdcmdline% " C:\test>(echo 2: %, %var1%, %var2%, !var1!, ^!var1^! !var2!, %cmdcmdline% ) | more 2: %, value1, %var2%, !var1!, !var1! !var2!, C:\Windows\system32\cmd.exe /S /D /c" ( echo 2: %, %var1%, %var2%, !var1!, ^!var1^! !var2!, %cmdcmdline% )" C:\test>for %a in (Z) do (echo 3: %a %, %var1%, %var2%, !var1!, ^!var1^! !var2!, %cmdcmdline% ) | more C:\test>(echo 3: Z %, %var1%, %var2%, !var1!, ^!var1^! !var2!, %cmdcmdline% ) | more 3: Z %, value1, %var2%, !var1!, !var1! !var2!, C:\Windows\system32\cmd.exe /S /D /c" ( echo 3: Z %, %var1%, %var2%, !var1!, ^!var1^! !var2!, %cmdcmdline% )" C:\test>( echo 4: part1 set "var2=var2Value set var2 echo " set var2 ) 4: part1 var2=var2Value " var2=var2Value C:\test>( echo 5: part1 set "var2=var2Value set var2 echo " set var2 echo --- begin cmdcmdline --- echo %cmdcmdline% echo --- end cmdcmdline --- ) | more 5: part1 var2=var2Value & set var2 & echo --- begin cmdcmdline --- C:\Windows\system32\cmd.exe /S /D /c" ( echo 5: part1 & set "var2=var2Value var2=var2Value & set var2 & echo " & set var2 & echo --- begin cmdcmdline --- & echo %cmdcmdline% & echo --- end cmdcmdline --- )" --- end cmdcmdline --- C:\test>( echo 6: part1 rem Only this line remarked echo part2 ) 6: part1 part2 C:\test>(echo %cmdcmdline% & ( echo 7: part1 rem This kills the entire block because the closing ) is remarked! echo part2 ) ) | more
- 3 回答
- 0 关注
- 663 浏览
添加回答
举报