How to Batch Rename File Extensions with One Script

In some cases, we may need to batch rename file extensions, such as changing all .txt files to .md. This article will detail how to achieve this using batch files (.bat) and Shell scripts, including detailed steps and user interaction prompts.

Batch File Implementation

We will write a batch script (.bat) that will guide the user step-by-step through the process of batch renaming file extensions.

1. Create the Batch File

Create a file named rename_extension.bat and copy the following content into it:

Terminal window
1
@echo off
2
setlocal enabledelayedexpansion
3
4
rem Step 1: Prompt user for the directory
5
set "dir="
6
set /p dir="Please enter the directory you want to modify: "
7
8
rem Step 2: Ask if user wants to include subdirectories
9
set "include_subdirs="
10
set /p include_subdirs="Do you want to include all subdirectories? (y/n): "
11
12
rem Step 3: Prompt user for the original and new file extensions
13
set "orig_ext="
14
set /p orig_ext="Enter the original file extension (without dot): "
15
set "new_ext="
16
set /p new_ext="Enter the new file extension (without dot): "
17
18
rem Step 4: Perform the renaming operation
19
if /i "%include_subdirs%"=="y" (
20
rem Include subdirectories
21
for /r "%dir%" %%f in (*.%orig_ext%) do (
22
set "filepath=%%f"
23
ren "%%f" "%%~nf.%new_ext%"
24
)
25
) else (
26
rem Do not include subdirectories
27
for %%f in ("%dir%\*.%orig_ext%") do (
28
set "filepath=%%f"
29
ren "%%f" "%%~nf.%new_ext%"
30
)
31
)
32
33
echo All matching files have been renamed.
34
pause

2. Run the Batch File

Double-click the rename_extension.bat file, and follow the prompts to input the required information. This will batch rename the file extensions in the specified directory.

Shell Script Implementation

We will write a Shell script (.sh) that will guide the user step-by-step through the process of batch renaming file extensions.

1. Create the Shell Script

Create a file named rename_extension.sh and copy the following content into it:

1
#!/bin/bash
2
3
# Step 1: Prompt user for the directory
4
read -p "Please enter the directory you want to modify: " dir
5
6
# Step 2: Ask if user wants to include subdirectories
7
read -p "Do you want to include all subdirectories? (y/n): " include_subdirs
8
9
# Step 3: Prompt user for the original and new file extensions
10
read -p "Enter the original file extension (without dot): " orig_ext
11
read -p "Enter the new file extension (without dot): " new_ext
12
13
# Step 4: Perform the renaming operation
14
if [ "$include_subdirs" = "y" ] || [ "$include_subdirs" = "Y" ]; then
15
# Include subdirectories
16
find "$dir" -type f -name "*.$orig_ext" -exec bash -c 'mv "$0" "${0%.*}.'$new_ext'"' {} \;
17
else
18
# Do not include subdirectories
19
find "$dir" -maxdepth 1 -type f -name "*.$orig_ext" -exec bash -c 'mv "$0" "${0%.*}.'$new_ext'"' {} \;
20
fi
21
22
echo "All matching files have been renamed."

2. Grant Execution Permission

Run the following command in the terminal to grant execution permission to the script:

Terminal window
1
chmod +x rename_extension.sh

3. Run the Shell Script

Run the following command in the terminal and follow the prompts to input the required information. This will batch rename the file extensions in the specified directory:

Terminal window
1
./rename_extension.sh

Summary

This article explained how to batch rename file extensions using batch files (.bat) and Shell scripts, providing detailed steps and user interaction prompts. With these scripts, you can easily batch modify file extensions, improving work efficiency.