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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#1 Post by Endoro » 26 Jan 2014 15:12

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?

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

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

#2 Post by carlos » 26 Jan 2014 15:39

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.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

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

#3 Post by carlos » 26 Jan 2014 15:48

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


aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 26 Jan 2014 16:14

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#5 Post by dbenham » 26 Jan 2014 16:19

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#6 Post by Endoro » 26 Jan 2014 16:36

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 :)

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

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

#7 Post by carlsomo » 26 Jan 2014 16:54

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

Post Reply