Order of DIR options and file names

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
May 30, 2008
206
1
#1
Does the DIR command allow options to be placed after the file/directory name specifications? It does seem to cause some problems when you put options last at least.


In the example below the /b switch is only applied to dir2.


C:\temp\test>dir dir1 dir2 /B

Volume in drive C is Local Disk Serial number is 60cd:fa66
Directory of C:\temp\test\dir1\*

20.08.2008 9:42 <DIR> .
20.08.2008 9:42 <DIR> ..
20.08.2008 9:42 13 test1
20.08.2008 9:42 13 test2
20.08.2008 9:42 13 test3
39 bytes in 3 files and 2 dirs 12 288 bytes allocated
8 406 994 944 bytes free
name1
name2
name3


But when you put it first it is applied to both dir1 and dir2.


C:\temp\test>dir /B dir1 dir2
test1
test2
test3
name1
name2
name3


When you read the help entry for DIR it seems to suggest you should put all options before all file names. Sometimes, in batch files and aliases, it would be easier if you could put the options last though.
 
#2
nikbackm wrote:
| Does the DIR command allow options to be placed after the
| file/directory name specifications? It does seem to cause some
| problems when you put options last at least.
| ...
| When you read the help entry for DIR it seems to suggest you should
| put all options before all file names. Sometimes, in batch files and
| aliases, it would be easier if you could put the options last though.

Less convenient or not, in all descendants of COMMAND.COM (CMD.EXE,
4DOS.COM, 4NT.EXE, TCMD32.EXE, TCC.EXE, etc.) the well documented general
rule for all commands is that options must precede parameters. In some
commands some options may be placed between paramters; they first parameter
they will affect is either the one immediately preceeding it or immediately
following it, depending on the command and option, and all parameters
thereafter, until superceded by a converse option, if any. The /B and /A
options of COPY are the clearly docuented example of the special use.
Another documented rule, applicable to JPsoft command processors only, is
that the "range" options must be first.

Another pair of rules that is very rarely obeyed:
- each option must have its own switch character (slash / by default)
- each option must be separated by white space from all other command
elements, including other options.

In many - actually most - instances the command processor accepts incorrect
syntax and does what I mean (DWIM). Observing the option separation rules is
usually not required. Using the /N option at the end of the command line to
show what the results of one of the file manipulation commands would be but
Not performing the command works fine, and is particularly useful in
interactive work - I can test the command by putting /N at the end, and if
it reports the desired action, I can recall it, delete the last two
characters, and do it by just 3 keystrokes.

However, unambiguous interpretation sometimes requires strict observation of
the rules, esp. the most basic one (options before parameters). I suspect
the DIR command's behavior you reported is due to the option letter being
one of the letters A and B, with require special handling in the COPY
command. Undoubtedly this special processing is used in file manipulation
commands other than COPY where it is harmless - if you had obeyed the syntax
rules.
--
Steve
 

rconn

Administrator
Staff member
May 14, 2008
10,101
85
#3
nikbackm wrote:

> Does the DIR command allow options to be placed after the file/directory
> name specifications? It does seem to cause some problems when you put
> options last at least.
Only as a special case when you have a single argument. Since DIR
supports multiple arguments, options apply to the following argument(s),
which allows you to use different options for each argument.

Rex Conn
JP Software