Skip to content

Batch Script Listing Folder Contents

Learn how to use the dir command in Windows batch scripts to list files and folders, including advanced options like filtering by attributes, sorting, and recursive directory traversal.

Listing the contents of folders can be done with the dir command. This command displays the files and directories available in the current directory. The dir command also shows the date and time of the last change and the size of the file.

dir [drive:][path][filename] [/a[[:]attributes]] [/b] [/c] [/d] [/l] [/n] [/o[[:]sortorder]] [/p] [/q] [/r] [/s] [/t[[:]timefield]] [/w] [/x] [/4]
Options Description
[drive:][path][filename] Specifies drive, directory, or files to list
/a Displays files with specified attributes.
attributes d Directories
r Read-only files
h Hidden files
a Files ready for archiving
s System files
i Not content indexed files
l Reparse Points

You can use any combination of these values, but don't separate your values using spaces.
You can use a hyphen - or a colon : as a prefix to mean not.
/b Uses bare format (no heading information or summary).
/c Displays the thousand separator in file sizes. This is the default. Use /-c to disable the display of the separator.
/d Same as wide but files are list sorted by column.
/l Uses lowercase.
/n New long list format where filenames are on the far right.
/o Lists by files in sorted order.
sortorder n By name (alphabetic)
s By size (smallest first)
e By extension (alphabetic)
d By date/time (oldest first)
g Group directories first
- Prefix to reverse order
/p Pauses after each screen is full of information.
/q Displays the owner of the file.
/r Displays alternate data streams of the file.
/s Displays files in the specified directory and all subdirectories.
/t Controls what time field is displayed or used for sorting.
timefield c Creation
a Last Access
w Last Written
/w Uses wide list format.
/x Displays the short names generated for non-8dot3 file names. The display is the same as the display for /n, but the short name is inserted before the long name.
/4 Displays years in four-digit format.
/? Displays help at the command prompt.

Examples

The following command lists any file that ends with the .exe file extension.

dir *.exe

The following command uses multiple filespecs to list any files ending with .txt and .doc in one command.

dir *.txt *.doc

It lists only the directories present in the current directory. To move to one of the listed directories, use the cd command.

dir /ad

Lists the files in the directory that you are in and all subdirectories after that directory. If you are at root C:\>, type this command, this will list to you every file and directory on the C: drive of the computer.

dir /s

If the directory has lots of files and you cannot read all the files as they scroll by, you can use the following command and it displays all files one page at a time.

dir /p

If you don't need file information you can use the following command to list only the files and directories going horizontally, taking as little space as needed.

dir /w

The following command will list all the files and directories in the current directory and the subdirectories, in wide format and one page at a time.

dir /s /w /p
Navigation