type nul >con suppresses output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sponge Belly
Posts: 221
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

type nul >con suppresses output

#1 Post by Sponge Belly » 26 Mar 2014 19:08

Hello All! :-)

I stumbled across this the other day:

Code: Select all

(cmd /d /u /c type nul >con
echo(first
echo(second
echo(third)


Only the first echo is displayed. The other two are suppressed. :?:

Can anyone explain what’s going on here?

TIA!

- SB

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: type nul >con suppresses output

#2 Post by jeb » 26 Mar 2014 20:09

I see this on my Win7 x64 system

Output wrote:first##second##third

Instead of #, an empty box is displayed

If I redirect this to a file, the file contains
Redirected file wrote:first
second
third


jeb

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: type nul >con suppresses output

#3 Post by foxidrive » 26 Mar 2014 22:02

I see the same as jeb on Windows 8.1

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: type nul >con suppresses output

#4 Post by Dos_Probie » 27 Mar 2014 03:25

On Win7 and 8 It shows

Code: Select all

first♪◙second♪◙third♪◙

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: type nul >con suppresses output

#5 Post by penpen » 28 Mar 2014 16:17

The characters "♪◙" are nothing else but the glyphs of "\r\n" (carriage return, newline), so the output is not wrong:
It is only rendered in a wrong way.
one possibility is that the cmd.exe parser may parse the full block and then executes it,
AND the cmd.exe then may 'occupy' all tokens following and executes it as a single command, so the result equals:

Code: Select all

>con cmd /C echo first&echo second&echo third
The wrong rendering of "\r\n" is caused (somehow) by the combination of ">con" and "cmd".
Maybe the inner cmd ("cmd /c") marks these characters somehow as 'consumed' when processing special characters,
so the outer (batch file) renders its glyphs, or ... (multiple possible answers: one as good as another).

penpen

Sponge Belly
Posts: 221
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: type nul >con suppresses output

#6 Post by Sponge Belly » 31 Mar 2014 02:07

Hi Penpen,

Thanks for the explanation. I was hoping one of you gurus might find an ingenious use for it, but it didn’t turn out that way.

This next little curiosity, however, might prove to be more fruitful. First, create a test file:

Code: Select all

>a2z.txt echo(the quick brown fox jumps over the lazy dog


And then try this:

Code: Select all

cmd/d /u /c type a2z.txt >con


Outputs only the first character (t) in the file. Next, put it all in a loop along with a couple of pauses:

Code: Select all

for /f delims^=^ eol^= %%c in ('
cmd /d /u /c type a2z.txt ^| (pause^>nul^&pause^>nul^&findstr "^"^)
') do echo(%%c


The output is h, the second character in the file. The snippet above could be easily extended to extract the nth character of a file… so long as the character isn’t CR, LF, or NUL.

- SB

Post Reply