I'm testing if I can append to a file using
type nul >> file
If the file is read-only, a 'Access is denied.' message is displayed,
but if I try
type nul >> file 2>nul
the 'Access is denied.' message is still displayed. I get the same
behavior from other commands as well.
I did some searching and found that adding surrounding parentheses
fixes the problem, but I don't know why.
(type nul >> file) 2>nul
Can anyone explain why this works?
Thanks,
John
Redirection isn't functionality provided by the type command. It's
functionality provided by the command processor. Adding 2>nul to the
type command without parentheses means send the type command's stderr
output to nul, but type doesn't generate any errors itself because it
happily reads nothing from nul and writes nothing. It's the command
processor that has issues trying to open a read-only file for
appending.
Parenthesizing commands means treat it as a command processor instance
(I'm trying to avoid Unix terminology, and I don't know the proper
Windows terminology if there is any), so the 2>nul applies to that
command processor instance rather than to the type command. Since it's
the command processor instance that issues the error message,
redirecting the parenthesized command's stderr works.
This is precisely define behavior under Unix systems, in which
cat /dev/null >> file 2> /dev/null
also produces an error message while
(cat /dev/null >> file) 2> /dev/null
doesn't, just like you experienced, but under Unix parentheses indicate
subshell processes. Online help for Windows XP only mentions that
parentheses may be used for command grouping. It doesn't mention
anything about parentheses creating child processes or redirection. But
you can't expect a poor company like Microsoft, with a mere US$40
billion in cash reserves, to be able to provide detailed documentation.