Why no time in TIME.TXT :- echo %time% > TIME.TXT > NUL

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Why no time in TIME.TXT :- echo %time% > TIME.TXT > NUL

#1 Post by alan_b » 17 Jul 2013 03:12

Why does NUL capture the text I intend for TIME.TXT ?
I only seem to get the result I want if I use parenthesis, i.e.

Code: Select all

( echo %time% > TIME.TXT ) > NUL


I wasted an hour or two before I tried the parenthesis, and I would like to FULLY understand :-
Why I need the parenthesis ;
Any convenient alternatives ;
Any further "gotchas" that I might stumble across

Reason I stumbled into this problem :-
I was echoing a messages to create various single line files in the current directory.
When the file name was short then all was well.
When the file name was long then there was an error message that with typical Microsoft Stupidity stated :-
"The system cannot find the path specified."
CMD.EXE was STANDING on the path that it was using - it did not have to go and find it :roll:
So naturally I redirected the error message to NUL.
UNFORTUNATELY EVEN IF the file name was short and the file could have been created,
then the message intended for the file was redirected to NUL instead.

N.B. The purpose of the exercise was to start with a single folder on a long path about 250 characters long,
and build in that folder a series of files with name of increasing length until reaching the Windows limit of 260 characters including the terminating NUL.


Incidentally, I had forgotten the correct term for "brackets" so I looked up
http://www.dostips.com/DtCodeSnippets.p ... ndGrouping
Parentesis is the word I found above - which earns a red squiggle from my spelling checker, and Google takes me to foreign sites such as Spanish, and suggests
Parenthesis

Regards
Alan

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

Re: Why no time in TIME.TXT :- echo %time% > TIME.TXT > NUL

#2 Post by penpen » 17 Jul 2013 06:12

Case 1:

Code: Select all

 echo %time% > filename > NUL 

The command processor (CP) executes this atomic expression:
At first it redirects the streams by handles, and then executes the command.

So if no error occurs the CP:
1. redirects the standard output stream (STDOUT) to a FileOutputStream (filename) (Wrong: See jebs next post.)
2. redirects STDOUT to the NUL device.
3 executes the command

So NUL is in this case the STDOUT of this program.


(This following will not happen on "echo %time% > filename > NUL": See jebs next post)
If the "> filename" doesn't success then the following happens:
1. redirects the standard output stream (STDOUT) to a FileOutputStream (Filename) fails
2. redirects STDOUT to the NUL device.
3 report error message to STDOUT, command is not executed

Nevertheless, on a single redirection to file step 2 and 3 would happen as described

You can verify it:

Code: Select all

set "test=test" > aaaaaaaaaaaaa...512 times...a 
set test


Case2:

Code: Select all

 (echo %time% > filename) > NUL 

The CP executes this as a complex expression:
At first it executes the the block with the redirection "(...) > NUL",
then the CP executes the commands within the block.

So if no error occurs the CP:
1. redirects STDOUT of the block to the NUL device.
2. executes the commands inside
3. redirects the standard output stream (STDOUT) to a FileOutputStream (filename)
4. executes the (echo) command

So FileOutputStream (filename) is in this case the STDOUT of this program.

If the "> filename" doesn't success then the following happens:
1. redirects STDOUT of the block to the NUL device.
2. executes the commands inside
3. redirects the standard output stream (STDOUT) to a FileOutputStream (filename) this failes
4. report error message to STDOUT, (echo) command is not executed


I have left out some details, but that's it.

penpen

Edit: Corrected/marked the errors.
Last edited by penpen on 06 Aug 2013 06:49, edited 1 time in total.

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

Re: Why no time in TIME.TXT :- echo %time% > TIME.TXT > NUL

#3 Post by jeb » 17 Jul 2013 07:03

Btw you can see what happens with the magic ECHO ON

Code: Select all

@echo ON
echo hallo > WhatEverYouWant > NUL


And you will see that it's collapsed to

Code: Select all

echo hallo > NUL


The parser only allows one redirection per handle, and the last one wins.
And all other redirections are removed without any checking against syntax nor file existance.

Then even this works

Code: Select all

@echo ON
echo hallo > aaaaaaaaaaaaaaaa512times_or_more:?* > NUL

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

Re: Why no time in TIME.TXT :- echo %time% > TIME.TXT > NUL

#4 Post by penpen » 17 Jul 2013 08:24

I thought, I have tested that,... but i only tricked myself out when verifying it:

Code: Select all

Z:>echo hallo > aaaaaaaaaaaaaaaa512times_or_more:?* >>  test.txt
Z:>type test.txt
Der Befehl "set"test=hallo"" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.


But the content of the file was not from this run, so i have derived false information.
Sorry for that misinformation.

penpen

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Why no time in TIME.TXT :- echo %time% > TIME.TXT > NUL

#5 Post by alan_b » 17 Jul 2013 09:31

Thanks for the replies.

I wildly experimented to find some sort of work-around before I started this topic.
I am much happier that I have a viable solution now that I understand what is happening.

Regards
Alan

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Why no time in TIME.TXT :- echo %time% > TIME.TXT > NUL

#6 Post by Samir » 17 Jul 2013 10:16

Very interesting. I remember from my C class that this would be some sort of order of operations and nesting, but didn't think that was possible in batch files. I'm sure I needed this a time or two before. 8)

Post Reply