Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

features of DIR

26 views
Skip to first unread message

Paul Emmons

unread,
Feb 22, 2018, 8:01:46 PM2/22/18
to
I just discovered that, in Windows-XT, DIR returns errorlevel 1 if no files
are found corresponding to the specifications submitted. (This is not true
in Windows 98). I was about to kick myself for not knowing this long
since, but the help for DIR does not document this useful feature.

In this case, "File Not Found" is displayed, presumably on the device
STDERR. If the output of DIR is redirected to a file, this message does
not appear in the file.

My first question: I am using DIR in a batch file and am handling the file-
not-found condition by means of the errorlevel. Is there a way to suppress
the display of the message "File Not Found"?

Second question: What other commands might now return errorlevel codes that
didn't use to do so?




Auric__

unread,
Feb 22, 2018, 10:55:26 PM2/22/18
to
Paul Emmons wrote:

> I just discovered that, in Windows-XT, DIR returns errorlevel 1 if no files
> are found corresponding to the specifications submitted. (This is not true
> in Windows 98). I was about to kick myself for not knowing this long
> since, but the help for DIR does not document this useful feature.
>
> In this case, "File Not Found" is displayed, presumably on the device
> STDERR. If the output of DIR is redirected to a file, this message does
> not appear in the file.
>
> My first question: I am using DIR in a batch file and am handling the file-
> not-found condition by means of the errorlevel. Is there a way to suppress
> the display of the message "File Not Found"?

dir 2>nul

> Second question: What other commands might now return errorlevel codes that
> didn't use to do so?

It may help to remember that the 9x line was seperate from NT. Dir has
*always* returned an errorlevel in NT. If you want to find out what else
does, just start experimenting, or script it:

%*
if errorlevel 1 echo YES

--
Back to our regularly scheduled madness.

JJ

unread,
Feb 23, 2018, 7:00:46 AM2/23/18
to
On Fri, 23 Feb 2018 01:01:40 GMT, Paul Emmons wrote:
> I just discovered that, in Windows-XT, DIR returns errorlevel 1 if no files
> are found corresponding to the specifications submitted. (This is not true
> in Windows 98). I was about to kick myself for not knowing this long
> since, but the help for DIR does not document this useful feature.

ERRORLEVEL is originally designed to provide access to the exit code of a
program (i.e. external command). But later, either since Windows NT 3.x or
4.0, some internal commands also generate an exit code.

Microsoft tend to remove old information, even though the information still
applies to newer software versions. The documentations in Windows NT 3.x or
4.0 probably contains more details about CMD.EXE, then the ones in Windows
2000 and newer versions.

> In this case, "File Not Found" is displayed, presumably on the device
> STDERR. If the output of DIR is redirected to a file, this message does
> not appear in the file.
>
> My first question: I am using DIR in a batch file and am handling the file-
> not-found condition by means of the errorlevel. Is there a way to suppress
> the display of the message "File Not Found"?

You'll have to redirect the STDERR handle as mentioned by Auric_.

> Second question: What other commands might now return errorlevel codes that
> didn't use to do so?

Most internal commands which have the possibility of causing an error due to
not finding something from the given parameter, will generate an exit code.
e.g. CALL, CD, DEL, FOR, FTYPE, GOTO, PUSHD, TYPE, VOL, etc. But since at
least one command generates an exit code only if a specific parameter is
used (e.g. SET /P), other commands may do it too.

While you can create a batch file to check each and every command
parameters, IMO, it would be a pretty large project - considering that there
are many command parameters in total.
0 new messages