Skip to main content

Batch Script Listing Folder Contents

Listing Folder Contents

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]
OptionsDescription
[drive:][path][filename]Specifies drive, directory, or files to list
/aDisplays files with specified attributes.
attributesd 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.
/bUses bare format (no heading information or summary).
/cDisplays the thousand separator in file sizes. This is the default. Use /-c to disable the display of the separator.
/dSame as wide but files are list sorted by column.
/lUses lowercase.
/nNew long list format where filenames are on the far right.
/oLists by files in sorted order.
sortordern 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
/pPauses after each screen is full of information.
/qDisplays the owner of the file.
/rDisplays alternate data streams of the file.
/sDisplays files in the specified directory and all subdirectories.
/tControls what time field is displayed or used for sorting.
timefieldc Creation
a Last Access
w Last Written
/wUses wide list format.
/xDisplays 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.
/4Displays 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