Home » How to Search for a Directory and Rename It in CMD

How to Search for a Directory and Rename It in CMD

You can use the following syntax in Command Prompt (CMD) to search for a directory by name and rename it:

for /D %G in ("directory_path\directory_name_pattern") do ren "%G" "new_directory_name"

This particular example will search for directories that match the specified pattern within the given path and rename them.

Note: Replace new_directory_name with the desired new name for the directory.

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

Example: Search for a Directory and Rename It using CMD

Suppose that we want to search for directories that start with temp_ within the following path and rename them to renamed_folder.

C:\Users\admin\Documents\cmd_script

We can type the following command to search for the directories and rename them:

for /D %G in ("C:\Users\admin\Documents\cmd_script\temp_*") do ren "%G" "renamed_folder"

Output: 👇️

CMD - Search for directory and rename it
CMD – Search for directory and rename it

We can see that the command searches for directories starting with temp_ within C:\Users\admin\Documents\cmd_script\ and renames them to renamed_folder.

Conclusion

I hope the above article on searching for directories and renaming them in CMD is helpful to you.

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