dir - Search for files

Maintained on

Searching for files is very useful when working with the command prompt or batch files.

In this article, we will show you how to search for files using the dir command.

Basics of the dir command

The dir command is used to list the files and folders in a directory.

You can find specific files by specifying search conditions or options.

Basic usage in the command prompt

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>dir

This command lists the files and folders in the current directory.

Basic usage in a batch file

@echo off
setlocal

dir

endlocal
exit

When you run this batch file, the files and folders in the current directory are displayed.

Searching for Files

You can use the dir command to search for specific files and folders by specifying search conditions.

Searching by File Name

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>dir \*.txt
2023/04/29 14:11 7 test2.txt
2023/04/29 14:11 7 text.txt
2 File(s) 14 bytes
0 Dir(s) 266,096,242,688 bytes free
C:\users\user>

This command lists all text files (.txt) in the current directory.

Searching by Folder

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>dir /s \*.txt
2023/04/29 14:11 7 test2.txt
2023/04/29 14:11 7 text.txt
2 File(s) 14 bytes
0 Dir(s) 266,096,242,688 bytes free
C:\users\user>

This command lists all text files (.txt) in the current directory and all subdirectories.

Options for the dir command

The dir command has several options. Here are some of the main options:

/a: Search by attribute

The /a option allows you to search by file or folder attribute.

For example, to search for hidden files, use the following command:

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>dir /a:h

/b: Simple display

The /b option allows you to display results in a simple format.

This displays only the file or folder names.

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b

/o: Sort options

The /o option allows you to sort search results by specific criteria.

For example, to sort by file name in ascending order, use the following command:

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>dir /o:n

There are several sorting criteria available with the /o option. Here are some of the main ones:

OptionDescription
/o:nSort by file name (ascending)
/o:-nSort by file name (descending)
/o:sSort by file size (ascending)
/o:-sSort by file size (descending)
/o:dSort by date and time (ascending)
/o:-dSort by date and time (descending)

Summary

In this article, we showed you how to search for files using the dir command.

The dir command is used to list the files and folders in a directory, and you can find specific files by specifying search conditions or options.

Be sure to take advantage of this command when working with the command prompt or batch files.

#PowerShell #Command Prompt #Batch File