Rename a folder on the command line. RENAME command for renaming files and directories

In Windows, there are many ways to rename multiple files at once - through Explorer, the command line, or PowerShell. There is a solution for both graphical interface lovers and those who prefer to work with commands.

Renaming through Explorer works quickly, but is not flexible enough. PowerShell has a lot of flexibility, but for a beginner this method can be intimidating. And if you need a powerful graphical tool, you will have to use a third-party program.

Conductor

Explorer has a rather unobvious way to quickly rename groups of files. First, collect all the necessary files in one folder. Switch to the Table view (Details) and sort the files into in the necessary order– The conductor assigns numbers according to the list starting from the top.

Select all the files you want to rename, right-click and select Rename. Enter a new name and press .

Explorer will append a number to this name for each file. A convenient way to bring all files to the same denominator, although not very flexible.

Command line

IN command line you can use the rename or ren command to bulk rename files. You can use the wildcard * to denote multiple files at once.

The easiest way to open a command window in the desired folder is to hold down , right-click on the folder and select “Open command window here”.

More often command rename used to change the extension of an entire group of files - this cannot be done in Explorer. The command below, for example, turns all .html files into .txt:

ren *.html *.txt

The command itself is not very functional, but it can be used in complex scenarios.

PowerShell

PowerShell offers many more options for renaming files in a command environment. With PowerShell, you can pass the output of one command (cmdlet, as it's called here) to another command, just like in Linux and other UNIX-like systems.

The main commands are Dir to get a list of files in the current folder and Rename-Item to rename an object (in in this case file). Just pass the output of Dir to the Rename-Item command and you're done.

After starting PowerShell, use the cd command to navigate to the folder with the necessary files. It is best to put all the files in one folder so as not to accidentally rename unnecessary ones.

Let's say we want to replace spaces in file names with underscores.

The following command lists the files in the current folder and passes it to the Rename-Item command, which replaces all spaces with underscores.

Dir | Rename-Item –NewName ( $_.name –replace “ “,”_” )

You can replace “ “ and “_” with other characters to rename the files differently.

Learn more about using Rename-Item to do more complex operations Can .

Third party utilities

If you need a powerful tool for bulk file renaming, but don’t want to mess around with the command line, you can use third-party utilities, for example. True, the interface of this application is quite confusing, since it provides a lot of capabilities that are usually only achievable using regular expressions and complex commands.

After installing the application, find and select the files to rename.

Changes the name of a file or set of files.

Syntax

rename[disk: ][path] file_name_1 file_name_2

ren[disk: ][path] file_name_1 file_name_2

Options

[disk: ][path] filename1 Specifies the location and name of the file or set of files to rename. file_name_2 Specifies a new file name. If wildcards (* and ?) are used, then file_name_2 specifies new names for files. When renaming files, you cannot specify a new drive or path. /? Display help on the command line.

Notes

  • Renaming files

    All files matching a given file name can be renamed. Team rename cannot be used to rename files on different drives or to move them to a different directory.

  • Using wildcards when renaming

    Wildcards (* and ?) can be used in parameters specifying names. If they are used in the parameter file_name_2, then the characters replaced by wildcards will be the same as in the parameter file_name_1.

  • The rename command will not work if file_name_2 already exists.

    If the file name specified by the parameter file_name_2, already exists, command rename will display the following message:

    Duplicate file name or file not found

Examples

Let's say that you need to change the name extensions of all files in the current directory with the extension .txt; for example, you need to replace the extension .txt with .doc. To make these changes, enter:

To rename the file or directory Chap10 to Part10, enter.

To rename one or more files, as well as directories using the command line, use the commands RENAME and MOVE.

The RENAME command has the following syntax: RENAME [drive:][path] filename1 filename2. In this case, “file_name1” defines the current name of the file (directory), and “file_name2” defines the name of the file (directory) that it will receive after renaming. For example, let’s create a file “text.txt” on drive “C”, then the command to rename this file to a file “file.txt” will look like this: rename text.txt file.txt

The RENAME command can rename not only file names, but also their extensions. For example, let's take the file "file.txt" that we obtained earlier by renaming the file "text.txt". Let's change the extension of this file from “txt” to “doc”: rename file.txt file.doc

Naturally, the RENAME command can change the file name and its extension at the same time. For example, let's rename the file “file.doc” to the file “text.txt”: rename file.doc text.txt

The RENAME command allows wildcards. Wildcards can be used in file names. For example, let’s create another file “format.txt” on drive “C” in addition to the existing file “text.txt”. Then, to rename (change the extension) both files, use the command: rename *.txt *.doc

If wildcards are used only in the filename2 parameter, the resulting file name will be the same as the source file name. For example, the command rename format.doc *.txt will change the file name "format.doc" to "format.txt"

If the file specified by the "file_name2" parameter already exists, then the RENAME command will not be executed. For example, let's try changing the file "text.doc" to "format.txt": rename text.doc format.txt

When renaming a directory, the “file_name1” parameter determines the current directory name, and the “file_name2” parameter determines the resulting directory name. For example, let’s create a folder “11” on drive “C”, then the command to rename this folder to folder “22” will look like this: rename 11 22

To secure of this material you can experiment with wildcards. For example, take the following command: rename *.* file.html . It seems pointless, because... all files in the current directory are renamed into one file "file.html". However this command executed perfectly. The fact is that all files are renamed one by one. As a result, the first (alphabetical) file will be renamed normally, and when you try to rename the 2nd file, the message “a file with the same name already exists or not found” will appear.

When using RENAME commands For the filename2 parameter, you cannot specify a different drive or directory. To do this, use the MOVE command.
The RENAME command is similar to REN. Both commands do similar actions and come from English. "rename" - rename.

Previous article: DEL command to delete files.
Contents: Command line.
Next article: MOVE Team.