Home / Microsoft / Microsoft DOS / [SOLVED] redirecting output
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: [SOLVED] redirecting output  (Read 1658 times)
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« on: September 11, 2009, 01:25:49 PM »

Hi, I've been browsing and searching for this for 1/2 an hour or so, but I still don't get it.
I have this batch file but isn't working.  From what I found, it should be correct, but.....

Here's the code:
Code: [Select]
@echo off

echo hiya 1>doesntexist\blabla.txt 2>error.txt

When I run is from command prompt:
Code: [Select]
c:\> test.bat
The system cannot find the path specified

Shouldn't the error message be written in error.txt?  Or did I understand wrong?

Thanks for any help

Two-eyes
« Last Edit: September 12, 2009, 07:59:14 AM by Two-eyes » IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

macdad-
Expert



Thanked: 39
Posts: 2,520


LoneWolf's Circuits
« Reply #1 on: September 11, 2009, 05:08:02 PM »

CMD always displays the errors in batch files, whenever it hits an error, it terminates the batch file
IP logged

If you dont know DOS, you dont know Windows...

Thats why Bill Gates created the Windows NT Family.
billrich
Guest
« Reply #2 on: September 11, 2009, 06:25:29 PM »

Two Eyes wrote:

<<"Here's the code:
Code: [Select]
@echo off

 
">>

Test the redirect at command prompt.


C:\>cd  doesntexist

C:\doesntexist>dir
 Volume in drive C has no label.
 Volume Serial Number is F4A3-D6B3

 Directory of C:\doesntexist

09/11/2009  07:23 PM    <DIR>          .
09/11/2009  07:23 PM    <DIR>          ..
09/11/2009  07:23 PM                 7 blabla.txt
               1 File(s)              7 bytes
               2 Dir(s)  306,860,113,920 bytes free

C:\doesntexist>type blabla.txt
hiya

C:\doesntexist>

IP logged
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #3 on: September 12, 2009, 02:25:17 AM »

@macdad-: ...should there have been a fullstop/period between "errors" and in batch files".  If that's the case, I understand.  Is there a work around or a way to continue the batch file? I believe I can evaluate %errorlevel%, i.e.
Code: [Select]
IF %errorlevel% NEQ 0 ECHO error %errorlevel% occurred>error.txt
But how do I get the actual message?

@billrich: the directory "doesnt exist" does not exist, ergo the name.  So it should output the error "the system couldnt find path specified".  But I want to capture that error message, and print it in error.txt.

Thanks Two-eyes
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #4 on: September 12, 2009, 02:51:54 AM »

@macdad-: ...should there have been a fullstop/period between "errors" and in batch files".  If that's the case, I understand. 

Quote from: macdad
CMD always displays the errors in batch files, whenever it hits an error, it terminates the batch file

No; the sentence makes perfect sense as it stands. The first clause states that CMD always displays the errors caused when batch files are run. The second and third, which are linked, state that when it [cmd.exe] encounters an error, [any error] the batch file is terminated. Perhaps I would have put a semicolon after 'files', but to make that a criticism would be excessively picky in my opinion.
IP logged


Proud to be European
T.C.
Beginner



Thanked: 11
Posts: 109




« Reply #5 on: September 12, 2009, 02:55:55 AM »

CMD always displays the errors in batch files, whenever it hits an error, it terminates the batch file

That's incorrect and a very dangerous assumption.  In lots of cases CMD issues the error message and continues with the next command.  Try this:

Code: [Select]
@echo off
cls

pushd doesntexist\

dir /a-d

The directory listing is produced for the default directory not the directory which was intended in the Pushd command.  If, instead of dir, the command had been del all sorts of brown stuff might occur.  With commands like Pushd and CD IMHO it's always best to use e.g.:

Code: [Select]
Pushd doesntexist\ || echo Error: Pushd failed - job terminated. && exit/b
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #6 on: September 12, 2009, 03:24:31 AM »

test.bat:
Code: [Select]
@echo off
echo hiya > doesntexist\blabla.txt

(1)
Console:
Code: [Select]
S:\>test.bat
The system cannot find the path specified.
S:\>

(2)
Console:
Code: [Select]
S:\>test.bat > err.txt
The system cannot find the path specified.
S:\>

err.txt:
Code: [Select]


(3)
Console:
Code: [Select]
S:\>test.bat 2> err.txt
S:\>

err.txt:
Code: [Select]
The system cannot find the path specified.



IP logged


Proud to be European
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #7 on: September 12, 2009, 04:28:27 AM »

@ Salmon Trout: aaaaaaah, I see.  You put one redirection in the batch file, and the other in the console.  That solves it.

Just a quick question: was my syntax wrong, i.e. I can't put two redirections in a line.  Cos I can do
Code: [Select]
something > myfile.txt 2>&1
and that's two redirections, unless I'm not understanding this well.

Thanks
Two-eyes
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #8 on: September 12, 2009, 04:34:15 AM »

I think the answer is that the 2> and  &1 notation is for programs & scripts that write error messages to stderr and console messages to stdout, so that we may combine stderr and stdout into one stream, but not for cmd.exe internal commands. An error in one of these stops the show so you have to do it the way I showed.

 
IP logged


Proud to be European
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #9 on: September 12, 2009, 04:42:18 AM »

Quote
is the simplest way I can think of putting it
Thanks....I'm new to this :)

When you say chain, do you mean
 1) it won't continue reading the current line ">myfile.txt 2>&1"
or
 2) it won't continue reading the next lines of code(if this were a batch file)

T-E
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #10 on: September 12, 2009, 05:23:51 AM »

1) it won't continue reading the current line ">myfile.txt 2>&1"
IP logged


Proud to be European
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #11 on: September 12, 2009, 05:34:04 AM »

But if it won't read that line, how will it send the error message to myfile.txt.  Or will it jump to the "2>"?

I also re-ask the question: can i put two redirections in a line of command?

Sorry, but I am like this.  I always want to know what the compiler/executer is doing. ;)  It's just my fascination how simple electric pulses do such complicated things :)

Thanks
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #12 on: September 12, 2009, 06:19:10 AM »

Code: [Select]
(echo hello > \notexistdir\file.txt) 2>error.txt
This works. You have to break the line into blocks using brackets.

This forces cmd.exe to process the echo statement first.
« Last Edit: September 12, 2009, 06:51:09 AM by Salmon Trout » IP logged


Proud to be European
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #13 on: September 12, 2009, 06:24:58 AM »

ahaaaaa.
Got it
Thanks :D
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #14 on: September 12, 2009, 06:26:16 AM »

Break the line. See edited post above.

Alternative: (note alternative placements of redirection)

Code: [Select]
2>error.txt (echo hello > \nodir\nofile.txt)
also note these formats for redirecting multiple lines

Code: [Select]
(
echo line 1
echo line 2
echo line 3
) > output.txt

Or...

Code: [Select]
> output.txt (
echo line 1
echo line 2
echo line 3
)

or for 1 line

Code: [Select]
> output.txt echo Hello!



« Last Edit: September 12, 2009, 06:51:48 AM by Salmon Trout » IP logged


Proud to be European
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #15 on: September 12, 2009, 07:21:34 AM »

oh, that's nice
Thanks :)
...
What do you know?  I tried the first code you sent, and it worked(obviously).
Then I tried this:
Code: [Select]
(ECHO hiya >doesnotexist\notextfile.txt) 2>error.txt
and got a text file called error.txt saying "the path couldnt be found" (ie the output i wanted originally)

dang...now I am even more curious ;D

Thanks :)
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #16 on: September 12, 2009, 07:50:52 AM »

See my (edited) post No. 12 above.
IP logged


Proud to be European
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #17 on: September 12, 2009, 07:58:47 AM »

ooops  :-[
Sorry, but you edited it twice, so I thought you meant the first edit.....my bad

Thanks

Two-Eyes
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #18 on: September 12, 2009, 08:06:49 AM »

ooops  :-[
Sorry, but you edited it twice, so I thought you meant the first edit.....my bad

Thanks

Two-Eyes

No, my bad, I forgot I had edited it twice (I can't remember what I wrote first!)
IP logged


Proud to be European
Salmon Trout
Sage



Thanked: 546
Posts: 7,952

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #19 on: September 12, 2009, 08:29:23 AM »

I think the answer is, that you know where the echo command ends but cmd.exe does not

ECHO hiya >doesnotexist\notextfile.txt 2>error.txt

it thinks this is the filename to echo hiya into:

doesnotexist\notextfile.txt 2>error.txt

But this way it knows

(ECHO hiya >doesnotexist\notextfile.txt) 2>error.txt



IP logged


Proud to be European
Two-eyes
Topic Starter
Intermediate



Thanked: 4
Posts: 166




« Reply #20 on: September 12, 2009, 11:53:33 AM »

ahhh yes.  The assumption "what I know, it knows".  I fall quite a few times in that. :)
IP logged

Quote
I believe the bushes in my yard will BURN before God picks up a PC to send a message

Pages: 1 2 [All] - (Top) Print 
Home / Microsoft / Microsoft DOS / [SOLVED] redirecting output « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.154 seconds with 19 queries.