| by Arround The Web | No comments

Batch File Rename: A Guide on Renaming Files through Batch Scripts

A batch file is essentially just a text file with a list of commands that a Windows command interpreter will run. One task that can be automated with the assistance of batch files is the file renaming. Batch scripts can be a quick and efficient way to rename many files simultaneously, especially when managing many files. You will be guided step-by-step through the batch file renaming procedure in this tutorial.

The user or developer can use the REN or RENAME command to rename the files using a batch script. The syntax for this command is:

REN [OLD_ FILE_ NAME] [NEW_FILE_NAME]

Or

RENAME [OLD_ FILE_ NAME] [NEW_FILE_NAME]

Example 1: Renaming a Single File

As shown in the picture, we need to use a batch script to rename two files in our working directory called “Products.txt and Laptops.txt” to “MyProducts.txt and ShopLaptops.txt” using a batch script. The code for the batch script that uses the RENAME and REN commands to rename our files is as follows:

@echo off
REM Use the RENAME command to Rename the Products.txt file
RENAME Products.txt MyProducts.txt
REM Use the REN command to Rename the Laptops.txt file
REN Laptops.txt ShopLaptops.txt

Here is a snapshot of the working directory before running the batch file via the command prompt:

Both files’ names are changed upon the “Rename.bat” command line script’s successful execution:

Example 2: Renaming the File Extensions

The REN command can also be used to rename many files simultaneously. We can accomplish this by utilizing the wildcard character (*). For instance, the following batch file changes the extension of all files that end in “.txt” to “.bak”:

@echo off
REM Use the REN command to change the extensions of multiple
:: text files
REN *.txt *.bak
ECHO Successfully Renamed

Here is the result after executing the batch file using the command line:

Example 3: Renaming Multiple Files in a Folder

Step 1: Launch a Text Editor
Start by creating a new text document using a text editor such as Notepad, Notepad++ on Windows, or any other code editor of your choice. You can also use the integrated development environments (IDEs) for a more advanced scripting. One popular option is Notepad which is pre-installed on Windows. Press the “Win + R” keys, type Notepad, and hit “Enter” to launch the Notepad.

Step 2: Write the Batch Script
In order to prevent the commands from being displayed on the screen, the batch file is instructed in the first line, @echo off, to turn off echoing.

The second line, setlocal enabledelayedexpansion, enables the delayed expansion. This means that the variables will not be expanded until they are used.

The third line, set “working_directory=E:\Work\Rename”, sets the “working_directory” variable to the path of the directory where the files are located.

The fourth line, set “prefix=MYFILE_”, sets the “prefix” variable to the prefix that is added at the beginning of the new file names.

The fifth line, set “files_extension=.txt”, sets the “files_extension” variable to the extension that is added at the end of the new file names.

The sixth line, for %%F in (“%working_directory%\*%files_extension%”) do (, starts a loop that iterates through all the files in the directory that is specified by the “working_directory” variable. The wildcard character (*) matches all files. The “files_extension” variable ensures that only the files with the specified extension are included in the loop.

The file name is taken from the complete path of the currently opened file on the seventh line, set “file=%%nF”. The batch file is instructed to extract the file name without the extension by the %%nF syntax.

The eighth line, set “file=!file:%prefix%=!, removes the prefix from the file name. The !file:%prefix%=! syntax tells the batch file to replace the first occurrence of the “prefix” variable with an empty string.

The ren “%%F” “%prefix%!file!%files_extension%” on line 9 renames the current file to the new file name. Renaming files is done with the “ren” command. The old file name is the first argument, while the new file name is the second argument.

The tenth line, endlocal, ends the local scope of the variables. This is necessary because the “setlocal enabledelayedexpansion” command only affects the variables in the current scope.

So, this code renames all the files in the specified directory to new file names with the specified prefix and extension.

@echo off
setlocal enabledelayedexpansion
:: Set the directory where your files are located
set "working_directory=E:\Work\Rename"
:: Set the prefix and extension for the new file names
set "prefix=MYFILE_"
set "files_extension=.txt"
:: Loop through the files in the directory
for %%F in ("%working_directory%\*%files_extension%") do (
    set "file=%%~nF"
    set "file=!file:%prefix%=!"
    ren "%%F" "%prefix%!file!%files_extension%"
)
:: Clean up
endlocal

After running the batch file from the command prompt, all the text files in the working directory will change their names to include the “MYFILE_” prefix.

Example 4: Renaming the Files Based on a Pattern

We can also rename the files based on a pattern. For example, we want to replace all spaces with underscores in the filenames of a folder:

Three files in our working directory have spaces in their names. Using the following code, we can use the batch scripting to replace the spaces with underscores.

@echo off
for %%F in (*.*) do (
    set "file_name=%%F"
    set "New_File_Name=!file_name: =_!"
    ren "%%F" "!New_File_Name!"
)
ECHO File Names Successfully Updated
endlocal

Open the command prompt, navigate to the working directory, and then type the batch file name and its extension before pressing the “Enter” button. Alternatively, we can double-click on the batch file:

Here is a screenshot of the updated file name which now contains an underscore instead of a space. This is a straightforward batch script replacement method for thousands of files.

Example 5: Renaming the Files Using the Conditional Statement

The “if” statement can be employed to rename the files based on specific criteria as well. For example, the following batch script renames all files that contain the word “Test” and concatenates their name with the “_RENAME.txt” using a conditional “if” statement in the current working directory where the “RenameConditional.bat” file exists.

@echo off
rem This script will rename all files containing the word "Test" to "_RENAME.txt".
for %%f in (*.*txt) do (
    set "FILE_NAME=%%f"
    set "FILE_NEW_NAME=%%~nf_RENAME.txt"
   
    setlocal enabledelayedexpansion
    echo !FILE_NAME! | findstr /i /c:"Test" > nul
    if !errorlevel! equ 0 (
        ren "!FILE_NAME!" "!FILE_NEW_NAME!"
        echo Renamed: !FILE_NAME! to !FILE_NEW_NAME!
    )
    endlocal
)
ECHO File renaming completed.

We have two files that contain the “Test” word in their name.

When we execute the batch file using the command prompt, this outputs the following lines on the screen:

Here is the updated working directory as the files are renamed using the batch files:

Important Note: Be cautious when renaming the files in bulk, especially if you’re using wildcard characters like “*” to match multiple files. A backup of your files should always be there in case something goes wrong.

Benefits of Batch Renaming

Here are some of the benefits of using the batch renaming:

  • It can save a lot of time.
  • It can help to avoid making mistakes.
  • It can help to keep our files organized.

We highly recommend using a batch script if the developers need to rename multiple files. It is a quick and simple technique to complete the task.

Conclusion

Batch renaming is using a batch script to rename multiple files at once. Batch scripts are text files that contain commands that are executed by the computer. The “for” loop is a standard command that is used in batch scripts to iterate through a set of files. The “ren” command is applied to rename a file. Using a combination of the “for” loop and the “ren” command, we can easily rename multiple files simultaneously.

Share Button

Source: linuxhint.com

Leave a Reply