Rob van der Woude's Scripting Pages

Tips for using DIR

  1. When using DIR to generate a file list for further processing, keep in mind that the output of the DIR command varies with the operating system, its version and the language and country settings!
  2. Display long file names in Windows 95/98:

    DIR /B "%*"*

    This command may sometimes display more than one file name, though.
  3. Display directories sorted, subdirectories first, including hidden files (MS-DOS 5+):

    DIR /A/OGN

    on the command prompt, or:


  4. If you need to use DIR in batch files, prevent weird DIRCMD settings from ruining your batch file.
    A setting like DIRCMD=/P for example, might stop your batch file where you least expected it.
    So, to make sure DIR does exactly what you intend it to do, never assume any defaults!
    Instead, specify every option yourself, as in:

    DIR /-A /-B /-O /-P /-S /-W

    In NT 4, however, /-P and /-W don't do what they were supposed to do.
    On the contrary, they have the same effect as /P or /W.
    The best workaround in NT 4 is to use /P/-P and /W/-W (tip provided by Walter Zackery).
    In Windows 7 and 8, another, simpler workaround is to temporarily reset DIRCMD (tip provided by "Explorer"):

    DIR whatever
  5. Hide files for all DIR parameters:


    Files in C:\FOLDER will not be found even with DIR C:\ /A/S
    DIR C:\ /A
    will show C:\FOLDER itself, but not the files, unless you manage to change to that directory
  6. In MS-DOS 7.* (Windows 9*) DIR /Z will display short file names only.
    Combined with /S, however, subdirectory names will be displayed with their long names. (tip from Georg Pohl and Tom Lavedas)
  7. For OS/2 Warp 4 with Fixpack ?? and Windows 98 with ServicePack 1:
    DIR /V will display file attributes too.

    26-06-99 12:25          4.409      0 a---  batchfiles.html
    27-06-99 20:38          5.789      0 a---  batexamples.html
    27-06-99 23:00          5.548      0 a---  batlinks.html
    22-06-99 22:20          2.897      0 a---  batutils.html

    Windows 98:
    CONFIG   SYS           494         8.192  11-10-99  21:39  11-11-99       A     CONFIG.SYS
    IO       SYS       222.390       229.376  19-02-99   0:36  08-11-99  RHS        IO.SYS
    MSDOS    SYS         1.676         8.192  30-05-99   0:01  11-11-99  RHS        MSDOS.SYS

    Unfortunately, this switch was discontinued in Windows NT 4 and later.
  8. Windows NT 4 (and later)'s DIR /TA switch shows the "last accessed" time, instead of the "last modified" time.
    This doesn't work on FAT formatted drives, though.
    On a FAT formatted drive the /TA swich will display 00:00 for the time of every file and directory.
    That gives us a perfect way to check if a drive is FAT formatted:

    IF "%1"=="/?" GOTO Syntax
    VER | FIND "Windows NT" > NUL

    SET FAT=
    FOR /F "TOKENS=2,3* DELIMS= " %%A IN ('DIR/A/TA/P/-P/W/-W %~d1 ˆ| FIND ":" ˆ| FIND "-"') DO IF NOT "%%A"=="00:00" SET FAT=non-
    ECHO Drive %~d1 has %FAT%FAT file system

    ECHO IsFAT, Version 1.00 for NT
    ECHO Written by Rob van der Woude
    ECHO Usage: ISFAT [ drive ]


    Click to view source Click to download the ZIPped sources
    The /P/-P and /W/-W switches overrule DIRCMD /P and /W settings, assuring the proper output format for DIR (tip provided by Walter Zackery)

    FileSys.bat is a more advanced replacement for IsFAT.bat, not only checking for FAT, but for NTFS as well.
  9. Windows 98 with service pack 1 applied also provides a "last accessed view" for DIR with its /OA switch.
    This doesn't work on FAT drives either, of course, but contrary to NT 4 it will just show the "last modified" dates and times instead.
  10. Windows 98 (service pack 1) and Windows 2000 show dates with 4 digits years with their /4 switch.
  11. Windows 2000 shows the owner of a file with DIR's /Q switch:

    C:\>DIR /Q
     Volume in drive C is BOOTDISK
     Volume Serial Number is B00D-F10B

     Directory of C:\

    18-11-2001  18:17                  580 BUILTIN\Administrators comreads.dbg
    18-11-2001  18:17                  545 BUILTIN\Administrators comused.dbg
    01-01-2002  13:01       <DIR>          BUILTIN\Administrators Documents and Settings
    26-01-2002  11:06       <DIR>          BUILTIN\Administrators Program Files
    25-01-2002  21:15       <DIR>          BUILTIN\Administrators TEMP
    25-01-2002  20:53       <DIR>          BUILTIN\Administrators WINNT
                   2 File(s)          1,125 bytes
                   4 Dir(s)   3,813,990,400 bytes free

  12. There are 2 switches to make DIR display short (8.3 format) file names in Windows 2000, /X and /-N:

    D:\>DIR /X
     Volume in drive D is DATA
     Volume Serial Number is DADA-D15C

     Directory of D:\

    19-08-2005  22:42                    2 FILE~1.WIT      File.with very long extension
    30-03-2005  20:15                   43                 TEMP.TMP
    30-03-2005  20:15                  210                 test.bat
    19-08-2005  22:27                    2 VERYLO~1.TXT    Very long file name.txt
                   4 File(s)            257 bytes
                   2 Dir(s) bytes free


    which will display both short and long file names to the right, and

    D:\>DIR /-N
     Volume in drive D is DATA
     Volume Serial Number is DADA-D15C

     Directory of D:\

    FILE~1   WIT                  2 19-08-2005  22:42
    TEMP     TMP                 43 30-03-2005  20:15
    test     bat                210 30-03-2005  20:15
    VERYLO~1 TXT                  2 19-08-2005  22:27
                   4 File(s)            257 bytes
                   2 Dir(s) bytes free

    which will display only the short file names to the left.
    Note that with DIR /-N the short file names and extensions are displayed in separate fixed width columns, separated by one or more spaces, whereas DIR /X displays the short file names and extensions linked by a single dot.

    In Windows 2000 and later, use a FOR loop with %%~sA to display fully qualified short file names.
    In Windows 2000, however, sometimes the last 4 characters of the file name and extension seem to be appended to the actual fully qualified short file name — a bug in Windows 2000 SP4?
    I was unable to recreate the problem in Windows XP, nor in Windows 7.

    D:\Program Files\Perl\.cpan\build\Getopt-Mixed-1.008\lib\Getopt>DIR
     Volume in drive D is DATA
     Volume Serial Number is DADA-D15C

     Directory of D:\Program Files\Perl\.cpan\build\Getopt-Mixed-1.008\lib\Getopt

    13-12-2003  21:13       <DIR>          .
    13-12-2003  21:13       <DIR>          ..
    09-02-1996  02:05               25.646
                   1 File(s)         25.646 bytes
                   2 Dir(s) bytes free

    D:\Program Files\Perl\.cpan\build\Getopt-Mixed-1.008\lib\Getopt>FOR %A IN (*.*) DO @ECHO.%~sA

    D:\Program Files\Perl\.cpan\build\Getopt-Mixed-1.008\lib\Getopt>

page last uploaded: 2016-09-19, 14:56