Code: Select all
@echo off
call :getCR.FullBuffer CR1
call :getCR.Pipe CR2
setlocal EnableDelayedExpansion
echo( World!CR1!Hello&
echo( World!CR2!Hello&
pause
exit /b
:getCR.FullBuffer
setlocal DisableDelayedExpansion
if "%~1"=="" (set "ret=CR") else set "ret=%~1"
for /F %%Z in ('
@"%COMSPEC%" /e:on /v:on /q /d /c
set "line= "^^^&
(for /L %%Z in (1 1 12^) do set "line=!line!!line!"^)^^^&
echo(!line!!line:~0^,-2!^^^&echo(
') do (
(goto)2>nul
set "%ret%=%%Z"
)
:getCR.Pipe
setlocal
if "%~1"=="" (set "ret=CR") else set "ret=%~1"
set "CR="
for /F %%Z in ('
set CR^=^^!^&
"%COMSPEC%" /u /d /c "echo(&echo(("^|
"%COMSPEC%" /e:on /v:on /d /c "set CR= &set /p CR=&echo(%%CR%%CR%%CR%%"
') do (
(goto)2>nul
set "%ret%=%%Z"
)
endlocal & goto %0
I forgot about the problem of CMD AutoRun registry settings. with FOR /F CMD will not disable processing of AuroRun commands by passing the /D switch to the child instance, so if any thing there outputs a single character, not only the above functions will fail, but the whole batch code will be in an unpredictable state, and most probably will be terminated, depending on the depth of the call stack and the amount of output lines by the AutoRun command(s).
I don't understand why the folks coding the CMD didn't consider this, they did this for pipes so why not for FOR /F ? And this can break many batch codes out there, for instance Dave's getPID routine. Surely the practice of putting something in AutoRun is not common, but peaple do crazy things when they made aware of some feature, without knowing the possible consequences of doing so.
OK, complaining doesn't solve any thing, this is the replacement code, fortunately, for this case the workaround is simple.
Code: Select all
:getCR.FullBuffer
setlocal DisableDelayedExpansion
if "%~1"=="" (set "ret=CR") else set "ret=%~1"
for /F %%Z in ('
@"%COMSPEC%" /e:on /v:on /q /d /c
set "line= "^^^&
(for /L %%Z in (1 1 12^) do set "line=!line!!line!"^)^^^&
echo(!line!!line:~0^,-2!^^^&echo(
') do set "CR=%%Z"
setlocal EnableDelayedExpansion
for %%Z in ("!CR!") do (
(goto)2>nul
set "%ret%=%%~Z"
)
:getCR.Pipe
setlocal EnableDelayedExpansion
if "%~1"=="" (set "ret=CR") else set "ret=%~1"
set "CR="
for /F %%Z in ('
set CR^=^^!^&
"%COMSPEC%" /u /d /c "echo(&echo(("^|
"%COMSPEC%" /e:on /v:on /d /c "set CR=#&set /p CR=&echo(%%CR%%CR%%CR%%"
') do set "CR=%%Z"
if "!CR!"=="#" endlocal & goto %0
for %%Z in ("!CR!") do (
(goto)2>nul
set "%ret%=%%~Z"
)