Why redirection sometimes fails ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Why redirection sometimes fails ?

#1 Post by Ed Dyreen » 30 Aug 2012 07:40

Code: Select all

@echo off &ver

echo.this redirection fails
reg.EXE add "" /f 2>&1>nul
echo.this redirection succeeds
reg.EXE add "" /f 2>nul>nul

pause
exit

Code: Select all

Microsoft Windows XP [versie 5.1.2600]
this redirection fails

Fout: ongeldige sleutelnaam
this redirection succeeds
Druk op een toets om door te gaan. . .

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

Re: Why redirection sometimes fails ?

#2 Post by foxidrive » 30 Aug 2012 08:13

Ed Dyreen wrote:echo.this redirection fails
reg.EXE add "" /f 2>&1>nul



This could be confusing the parser as 1>nul is redirecting STDOUT to nul.

I think the order of redirection matters: IE if you do not explicitly set STDOUT >nul before setting 2>&1 then STDERR is going to be redirected to where STDOUT was going at the time, in this case to the console.

So 1>nul 2>&1 is the order to use, or EG:

reg.EXE add "" /f >nul 2>&1

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Why redirection sometimes fails ?

#3 Post by Ed Dyreen » 30 Aug 2012 08:32

Code: Select all

echo.this redirection succeeds
reg.EXE add "" /f>nul 2>&1
I hadn't thought of that, sweet :D

Thank u foxi !

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Why redirection sometimes fails ?

#4 Post by Squashman » 30 Aug 2012 19:36

Ed Dyreen wrote:

Code: Select all

echo.this redirection succeeds
reg.EXE add "" /f>nul 2>&1
I hadn't thought of that, sweet :D

Thank u foxi !

You just saw me doing that in another thread.
viewtopic.php?f=3&t=3708&p=19590#p19590

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Why redirection sometimes fails ?

#5 Post by Ed Dyreen » 30 Aug 2012 20:00

'
No I didn't :twisted:

I know DOS rearranges commands before it executes them.

Code: Select all

@echo on &prompt $G &ver

reg.EXE add "">nul 2>&1
>nul 2>&1reg.EXE add ""

reg.EXE add "" 2>&1>nul
2>&1>nul reg.EXE add ""

pause
exit
But I didn't realize switching the order would.

Code: Select all

Microsoft Windows XP [versie 5.1.2600]

> reg.EXE add "" 1>nul 2>&1

> reg.EXE add "" 1>nul 2>&1

> reg.EXE add ""  2>&1 1>nul

Fout: ongeldige sleutelnaam

> reg.EXE add "" 2>&1 1>nul

Fout: ongeldige sleutelnaam

> pause
Druk op een toets om door te gaan. . .
To be honest, I've examined my codes and apparently I've been doing it "right" often enough. It's only recently that I discovered why it's important to document why you do something the way you do it. As if you don't you'll forget. Seems I'm not an elephant after all. I've documented it and should be fine now 8)

Post Reply