Home » How to Find a Directory by Name Pattern in CMD

How to Find a Directory by Name Pattern in CMD

In Windows, directories do not have unique IDs that can be used to search for them directly. However, you can search for directories by name or other attributes using the [dir] command or PowerShell.

Suppose you want to find a directory by a specific attribute or name pattern, you can use the following syntax in Command Prompt (CMD):

dir /S /B "directory_path\directory_name_pattern"

In this example, the dir /S /B command searches for directories that match the specified pattern within the given path and its subdirectories.

The following example shows how to use this syntax in practice.

Example: How to Find a Directory by Name Pattern in CMD

Suppose that we want to search for directories that contain demo in their name within the following path:

C:\Users\admin\Documents\cmd_script

We can type the following command to search for the directories:

dir /S /B /AD "C:\Users\admin\Documents\cmd_script\*demo*"

The /AD flag specifies to search for directories only.

Output: 👇️

CMD - find directory by name pattern
CMD – find directory by name pattern

We can see that the command searches for directories demo within C:\Users\admin\Documents\cmd_script and its subdirectories and displays the full path of any matching directories.

Note: You can use the dir command followed by the path and the name pattern to search for any directory on your system.

Conclusion

I hope the above article on finding directories by name patterns in CMD is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.