You are not logged in.

#1 31 Oct 2013 14:52

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

What exactly is &3 stream and what can be used for?Is the same as &1?

It is detected by FOR in the same way as &1 (see the difference with &2) :

C:\>for /f %a in ('echo a^>^&2') do @echo ~%a~
a

C:\>for /f %a in ('echo a^>^&3') do @echo ~%a~
~a~

(just to mention the only accesible streams are &1,&2 (error stream) and &3)

Last edited by npocmaka (31 Oct 2013 14:56)

Offline

#2 31 Oct 2013 15:18

jeb
Member
From: Germany
Registered: 19 Nov 2010
Posts: 109

Re: What exactly is &3 stream and what can be used for?Is the same as &1?

You should read dbenhams discovery of streams.
That's great and it will open your eyes how redirection really works smile

Last edited by jeb (31 Oct 2013 15:18)

Offline

#3 31 Oct 2013 18:37

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 111

Re: What exactly is &3 stream and what can be used for?Is the same as &1?

File handles 3 - 9 are initially undefined, but they are actually available for use! There is a surprising, but logical reason why something like echo Hello world! >&3 appears on the screen (stdout).

The post that jeb linked shows the beginnings of my experiments that set me on the path to discovering how redirection works.

The actual working theory that explains everything is in two consecutive posts within the same thread, beginning at http://www.dostips.com/forum/viewtopic. … 612#p14612.

I recommend reading all the prose in those two posts first, ignoring the code and output. Then if you are interested, go back and read the code and output to trace how I "proved" my theory.

Finally, if you are so inclined, you can up vote my answer to StackOverflow question How to permanently redirect standard error back to the console again? big_smile


Dave Benham

Last edited by dbenham (31 Oct 2013 18:58)

Offline

#4 31 Oct 2013 22:50

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 111

Re: What exactly is &3 stream and what can be used for?Is the same as &1?

As far as "What is &3 good for?" Lots of things! smile

Undefined handles can be used to efficiently write to multiple files. For example, suppose I want to classify all integers from 1 to 10,000. I want to write odd numbers to odd.txt, even numbers to even.txt, and every 1000 I want to write to the screen so the user can keep track of the progress.

The classic method is to selectively redirect output of certain statements to the correct file using append mode. But this is quite slow because each append operation must reopen the file and move the file pointer to the end of the file.

@echo off
copy nul "odd.txt" >nul
copy nul "even.txt" >nul
2>nul (
  for /l %%N in (1 1 10000) do (
    set /a "1/(%%N %% 1000)" || (cls & echo %%N)
    set /a "1/(%%N %% 2)" && (>>"odd.txt" echo %%N) || (>>"even.txt" echo %%N)
  )
)

It is much faster to direct unused file handles 3 and 4 to the correct files outside the loop, and then within the loop selectively redirect to &3 and &4. The files are already open for writing with the file pointer at the end of file, so it is much faster.

@echo off
3>"odd.txt" 4>"even.txt" 2>nul (
  for /l %%N in (1 1 10000) do (
    set /a "1/(%%N %% 1000)" || (cls & echo %%N)
    set /a "1/(%%N %% 2)" && (>&3 echo %%N) || (>&4 echo %%N)
  )
)

For a sophisticated use of non-standard file handles, take a look at my SNAKE.BAT game. The game has multiple CMD.EXE processes running within the same console. I redirect file handles 8 and 9 to temporary files that are used as communication buffers between the two processes. The controller can write a message to >&9 and the main game process reads the mesage from <&9. Communication in the reverse direction uses &8. I have all this communication, yet it doesn't interfere with stdin, stdout or stderr. Pretty cool! cool


Another interesting application is swapping file handles. Suppose you want to swap stderr with stdout so that you can pipe your error messages to another process, but your stdout still goes to the screen. A third file handle is needed to execute the swap. Take a look at StackOverflow question: Is there a way to redirect ONLY stderr to stdout (not combine the two) so it can be piped to other programs


Dave Benham

Last edited by dbenham (04 Nov 2013 13:23)

Offline

#5 02 Nov 2013 21:50

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: What exactly is &3 stream and what can be used for?Is the same as &1?

You may also review the Access to SEVERAL files via Standard Handles post:

"Aacini wrote:

... a Batch file is limited to manage just two files: STDIN for input (SET /P) operations, and STDOUT for output (ECHO, etc.) operations. We could made good use of my program in a better way if we could access more than one input and output files in a Batch file. How to do that? In a relatively easy way: just connect the additional files to unused handles (3-9) and use the appropiate handle in the input (SET /P <&#) or output (ECHO >&#) commands.

The Batch file below merge the lines of 3 input files into one output file with larger lines:

@echo off
setlocal EnableDelayedExpansion
3<input2.txt 4<input3.txt (
for /F "delims=" %%a in (input1.txt) do (
   set line=
   rem Read from input2.txt (and write line from input1 to output.txt):
   set /P line=%%a <&3
   rem Read from input3.txt (and write line from input2 to output.txt):
   set /P line=!line! <&4
   rem Write line from input3 to output.txt:
   echo(!line!
)
) >output.txt

The same method may be used to generate several output files.

Antonio

Last edited by Aacini (02 Nov 2013 21:51)

Offline

#6 04 Nov 2013 08:38

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Re: What exactly is &3 stream and what can be used for?Is the same as &1?

Hey, thanks all.Still have no time to read these in details , but looks very interesting.

Offline

Board footer

Powered by