Page 1 of 1

Creating carriage return (CR, 0Dh) doesn't work in Windows 8

Posted: 26 Jan 2014 15:12
by Endoro
I tested the following code in XP Home x86 and Windows 8 Pro x64:

Code: Select all

@echo off &setlocal disableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
setlocal enableDelayedExpansion
for /l %%a in (1,1,10) do (
   <nul set /p "=!CR!%%a"
)


Output in XP is:

Code: Select all

10
C:\>


and in Windows 8:

Code: Select all

12345678910
C:\>

Who can confirm or reject this observation?

Re: Creating carriage return (CR, 0Dh) doesn't work in Windo

Posted: 26 Jan 2014 15:39
by carlos
In my Windows 8 I have the same output, but apparently the effect is disabled by set /p :

This:

Code: Select all

<nul set /p "=!CR!" >cr.txt


on Windows generates 0 byte file.

This is a new change feature of set /p, for the table comparision of dbenham.

Re: Creating carriage return (CR, 0Dh) doesn't work in Windo

Posted: 26 Jan 2014 15:48
by carlos
The effect using the 0d from a file:

this print:

Code: Select all

10


Code: Select all

@echo off &setlocal disableDelayedExpansion

call :create_0d.chr
for /l %%a in (1,1,10) do (
   type 0d.chr
   <nul set /p "=%%a"
)

Goto :Eof



:create_0d.chr
Rem Script made using BHX { code.google.com/p/bhx }
SetLocal EnableExtensions EnableDelayedExpansion
For %%# In ("0D.chr") Do Set "bin=%%~f#"
Del /A /F "%bin%" "%bin%.hs" "%bin%.da" "%bin%.pa" >Nul 2>&1
(
Echo 0D
)>>"%bin%.hs"
Set ".=Set o=CreateObject(#Scripting.FileSystemObject#):Set r="
Set ".=%.%o.OpenTextFile(#%bin%.hs#,1):Set w=o.OpenTextFile(#"
Set ".=%.%%bin%#,2,1):Do Until r.AtEndOfStream:d=r.ReadLine:Fo"
Set ".=%.%r i=1 To Len(d) Step 2:w.Write Chr(CByte(#&H#&Mid(d,"
Set ".=%.%i,2))):Next:Loop:r.Close:w.Close:Set w=Nothing:Set r"
Set ".=%.%=Nothing:Set o=Nothing"
Set "w2k=0" &Ver |Find " 2000" >Nul 2>&1 && Set "w2k=1"
Set ";=!.:#="!"
Echo !;!>"%bin%.da"
Certutil.exe -decodehex "%bin%.hs" "%bin%" >Nul 2>&1
If Not Exist "%bin%" Cscript.exe /B /E:vbs "%bin%.da" >Nul 2>&1
If Not Exist "%bin%" If %w2k%==0 (
Mshta.exe Vbscript:Execute^("%.:#=""%:Close"^) >Nul 2>&1) Else (
Echo Close>>"%bin%.da"
Echo ^<html^>^<head^>>"%bin%.pa"
Echo ^<hta:application windowstate="minimize"^>>>"%bin%.pa"
Echo ^<script type="text/vbscript" src="%bin%.da"^>>>"%bin%.pa"
Echo ^</script^>^</head^>^</html^>>>"%bin%.pa"
Mshta.exe "%bin%.pa" >Nul 2>&1)
Del /A /F "%bin%.hs" "%bin%.da" "%bin%.pa" >Nul 2>&1
Goto :Eof


Re: Creating carriage return (CR, 0Dh) doesn't work in Windo

Posted: 26 Jan 2014 16:14
by aGerman
I fear the problem is not that you can't create the carriage return but since Win7 are leading white spaces stripped from the set /p string.

Regards
aGerman

Re: Creating carriage return (CR, 0Dh) doesn't work in Windo

Posted: 26 Jan 2014 16:19
by dbenham
That is the expected behavior for Vista and Win7, as outlined in my table. If you look, you will see that I list the carriage return as one of the white space characters that gets stripped.

So, my guess is that Win8 is the same as Vista and Win7. But it would be nice if all the features were tested.


Dave Benham

Creating carriage return (CR, 0Dh) does work in Windows 8

Posted: 26 Jan 2014 16:36
by Endoro
Thank you, this is working:

Code: Select all

@echo off &setlocal disableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
setlocal enableDelayedExpansion
for /l %%a in (1,1,10) do (
   <nul set /p "=%%a!CR!"
)


Problem is solved :)

Re: Creating carriage return (CR, 0Dh) doesn't work in Windo

Posted: 26 Jan 2014 16:54
by carlsomo
This is the workaround I use in Win7 and does not require writing or reading from a file. Set/p will display backspaces.

The :Setup_etc function defines max line width. The :CR function accepts a number to move cursor left. It is also very useful to move the cursor back just a specified number of characters in a line for intra line editing. If a " " (space) char is passed to :CR rather than a num left argument it displays a leading space without a linefeed to the console. This does not obviously work well if writing to files since backspaces are output.

Code: Select all

:Non-Destructive backspace ; $b
:Destructive backspace ; $db
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "$b=%%a"
set "$db=%$b% %$b%"

:SetUp_Carriage_Return Num
set "BackSpaces="
set/a num=%~1
for /l %%a in (1,1,%num%) do call set "BackSpaces=%%BackSpaces%%%$b%"
exit/b

:CR numleft ; 'BackSpaces' must be defined, numleft must resolve to a positive integer or " "
setlocal enabledelayedexpansion
if "%~1" neq " " (
  set/a "num=%~1"
  for %%a in (!num!) do if %%a gtr 0 (
    call set "bkspc=%%BackSpaces:~0,%%a%%"
    <nul set/p="!bkspc!"
  )
) else (<nul set/p=".%$b% ")
endlocal&exit/b