≡ Menu

Dir command line options

dir command can be used to list the files from command prompt. This article explains the syntax for different usecases.

  • A simple dir command without any other arguments lists all the files/subfolders that exist in the current folder.
    dir
  • Lists the subfolders/files names in bare format.
    dir /b

    This command prints the file names. No other file meta data like file modified time, file size etc are not displayed.

  • List the files in the current folder and also the ones in the subfolders recursively.
    dir /s

Using wild cards with dir

Dir command accepts wild cards to display information only for the files that match the pattern specified. The below examples illustrate different use cases where we can use these wild cards

  • List files of certain type or based on extension
    For example to list all jpeg files in the current folder, we can run the below command.

    dir *.jpeg

    To list all excel files

    dir *.xls

    We can even specify multiple extension in dir command to list files of any of the types. To list all files created with Office applications like Word, Excel, Powerpoint etc we can run below command.

    dir *.docx *.xlsx *.pptx
  • List files beginning/ending with specific pattern
    List all files in the current folder whose names begin with ‘Picture-‘

    dir /S Picture-*

List file names based on type

dir command can list the file names and also the subfolders names. We can be selective and say that we want only names of the files to be listed or only the names of the subdirectories to be listed.

  • List only directories
    dir /A:D
  • List only files
    dir /A:-D

Display files based on file attributes

We can filter out which files should be listed in the dir command output based on read-only, system, hidden archive file attributes.

  • For example, to list read-only files in the current directory, the command is:
    dir /A:R
  • Similarly to display hidden files
    dir /A:H
  • For the opposite case of looking for files where an attribute is not set, we can append '-' to the attribute code. For example, to print the file names without archive attribute set, we can use the below command.
    dir /A:-A
  • Exclude Read-only files from the listing.
    dir /A:-R
  • Exclude hidden files from listing.
    dir /A:-H
  • Exclude system files from listing
    dir /A:-S

Print metadata of files using dir command

  • Find out who owns a file
    dir /Q

    Example:

    c:\>dir /Q 1.txt
    05/03/2015  01:12 AM           151,906 BUILTIN\Administrators 1.txt
    

    The above result of dir indicates that file 1.txt is owned by Administrators group.

  • Get created time of a file
    dir /TC
  • Find last accessed time of a file
    dir /TA
  • Find last modified time of a file
    dir /TW
8 comments… add one
  • Aniruddha

    How to get absolute path using command prompt ?

    • admin

      dir /s /b would print absolute path.

  • Pang

    I want to list all the empty subdirectories in a folder and then delete them. How do I do that using dir command

  • kiai

    Is it possible to get dir to print paths with spaces in a directory name?

    • kiai

      Answered the question… Now the real problem: Have a command that gives “failed to load external entity” for a file inside a directory with a space. Here’s the command:

      for /F %i in (‘dir /s /b D:\YourDirectory\filepath\Folio.xml’) do (C:\progra~1\xmlstarlet\xml.exe ed –inplace -d “//overlay[data/webViewUrl[contains(text(),’shopthis.mastercard’)]]” %i)

      Your help is appreciated!

  • Aurélien

    Is it possible to get the file icon with dir cmd?

    I would like to list files and extract icon to show them in my application.

  • Gopi

    I would like to get the last modified time stamp of the latest file in the directory like just the date and time field values alone into different variables. Something similar to using awk, cut commands in Unix.

  • mads hansen

    Special danish characters

    I routinely use

    dir /o /w > files.txt

    to create a text file containing the file names in a folder. I can easily edit that file for use as a table of content for the audio newspaper hosted on our site. The file names are so long, that dir lists them in one column, which is perfect.

    BUT: The files have danish special letters æ (html: æ) – ø (ø) – å (&aring) and their uppercase counterparts, Æ, Ø, Å. These come out wrong with the dir command, and have to be edited manually.

    Q: Is there a way to make the dir command output these letters correctly? (Windows 7 Pro, Danish version, computer is NOT connected to the internet…)

    /Mads Hansen

Leave a Comment