[Trick] Preliminary streams redirection

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

[Trick] Preliminary streams redirection

#1 Post by Dragokas » 24 May 2014 06:33

Code: Select all

:: Preliminary streams redirection.
:: Published on 09.12.2012 by Alex Dragokas.
@echo off

:: All future streams #1, #2 will be redirerect to a file 'out.txt'
echo. 1>&3 2>&4 3>out.txt 4>&3

:: testing StdOut (descriptor #1)
Echo testing

:: imitation StdErr (descriptor #2)
copy zz:\

:: try StdIn
set /p "some=Your input: "

:: show message on console (without redirection)
pause>con

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: [Trick] Preliminary streams redirection

#2 Post by Aacini » 24 May 2014 07:20


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

Re: [Trick] Preliminary streams redirection

#3 Post by dbenham » 24 May 2014 07:24

Yes, that is a nasty CMD.EXE design flaw that was investigated thoroughly beginning at viewtopic.php?p=13436#p13436. The behavior was finally explained in two consecutive posts at viewtopic.php?p=14612#p14612.

The "trick" really should be avoided because it permanently locks the output file. The exclusive lock will not be released until the cmd session terminates. I suppose it could be useful in some specialized circumstances, but as a general rule, I don't like it.


Dave Benham

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: [Trick] Preliminary streams redirection

#4 Post by Aacini » 24 May 2014 08:24

dbenham wrote:Yes, that is a nasty CMD.EXE design flaw...

Dave Benham

Well, this is not exactly a nasty design flaw...


This behaviour is caused by the method that cmd.exe uses to achieve redirections, as explained in the last post of that thread. That method is the logical, most efficient one, from DOS internals point of view (BTW, file redirections in Unix-like operating systems are achieved in the same way).

In other words: if you would need to write an .exe program that internally manage file redirections, your program would use the same method and hence it would show the same behaviour under the same circumstances...

Antonio

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: [Trick] Preliminary streams redirection

#5 Post by Dragokas » 24 May 2014 08:55

:)
Oh, thanks Aacini and dbenham.
I shall read that. :)
I'm surprise how many interestring researches you, experts, are maded all this time.
It's pity, I did not participated them.

So, I am also don't use this method.
Sometimes for a small scripts I prefer restarting like this:

Code: Select all

if "%~1"=="" (
  Echo Working...
  Call "%~dpnx0" 0 1>nul 2>&1
  Echo Ready...
  Goto :eof
)
:: main script


But with a file it also do an exclusive lock.
I use it in case where construction:

Code: Select all

(
icacls "%Cur%" /deny All:(DE,WDAC,WO,AS,WD,AD,WEA,DC,WA) /T /C
icacls "%Cur%" /grant All:(RC,S,GR,GW,GE,RD,REA,X,RA) /T /C
) 1>nul 2>&1

cause a critical error with closing a cmd:
/T was unexpected at this time.

Don't know why. Any ideas?

Post Reply