Open In App

How to Rename All Files of a Folder using Java?

Last Updated : 09 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When transferring files from the camera folder to a workspace where we would like to analyze the pictures, it becomes difficult to deal with long files and type them out again and again when testing them through code. Also, the number of files might be too large to manually rename each one of them. Hence, it becomes a necessity to automate the renaming process.

In this article, we are going to learn how to rename all files of a folder using Java.

Example: We might have file names like this:

Assume we have 50 files in the folder

"C:\Users\Anannya Uberoi\Desktop\myfolder":

Snapshot 1 (12-05-2025 11-57).png

Snapshot 2 (12-05-2025 11-57).png

Snapshot 3 (12-05-2025 11-57).png

Snapshot 4 (12-05-2025 11-57).png and so on.

Instead of manually renaming these files, we can rename all files in a folder with the help of a simple Java program. This way, we can rename the files to something easier like 1.png, 2.png and 3.png.

Now let's see how we are going to implement this in Java.

Problem:

We have a lot of files that are named something like this:

Snapshot 1 (12-05-2025 11-57).png

Snapshot 2 (12-05-2025 11-57).png

Snapshot 3 (12-05-2025 11-57).png


We aim to rename them to something like below:

1.png

2.png

3.png

Rename All Files of a Folder using Java

Solution:

To solve this problem, we are going to write a small Java program that will go through each file in a folder and rename it to a simple sequential number and keep the file extension like .png or .jpg.

Example:

Java
// Java program to demonstrate 
// how to rename all files
import java.io.File;
import java.io.IOException;

public class Geeks {
    
    public static void main(String[] args) throws IOException {
        
        // Set the folder path where your files are located
        String folderPath = "C:\\Users\\Anannya Uberoi\\Desktop\\myfolder";

        // Create a File object for the folder
        File folder = new File(folderPath);

        // Get a list of all files in the folder
        File[] files = folder.listFiles();

        // Check if there are any files in the folder
        if (files != null) {
            int counter = 1;

            // Loop through all files in the folder
            for (File file : files) {
                
                // Check if it's a file (not a folder)
                if (file.isFile()) {
                    String fileName = file.getName();
                    String fileExtension = "";

                    // Extract the file extension (e.g., .png, .jpg)
                    int dotIndex = fileName.lastIndexOf('.');
                    if (dotIndex > 0) {
                        fileExtension = fileName.substring(dotIndex);
                    }

                    // Create the new file name (e.g., 1.png, 2.png, etc.)
                    String newFileName = counter + fileExtension;

                    // Create a new File object with the new file name
                    File renamedFile = new File(folderPath + "\\" + newFileName);

                    // Rename the file
                    if (file.renameTo(renamedFile)) {
                        System.out.println("Renamed: " + fileName + " -> " + newFileName);
                    } else {
                        System.out.println("Failed to rename: " + fileName);
                    }

                    // Increment the counter for the next file
                    counter++;
                }
            }
        } else {
            System.out.println("No files found in the directory.");
        }
    }
}

Output:

Renamed: Snapshot 1 (12-05-2025 11-57).png -> 1.png
Renamed: Snapshot 2 (12-05-2025 11-57).png -> 2.png
Renamed: Snapshot 3 (12-05-2025 11-57).png -> 3.png

Explanation: In this example, first we need to define the folder path where the files are stored and then we are going to retrieve the files from that folder and then it loops through each file and extract its file extension. For each file, it creates a new name based on the counter and renames the file. After renaming the files, it print the renamed files or if a failure occurs it will print that renaming was not successful.

Important Points:

  • If the folder does not contain any files, the program will let us know with a message "No files found in the directory".
  • If a file fails to rename, the program will print an error message for that file.
  • The program works only for files and ignores subfolders. If you have subfolders in your folder, they won’t be renamed.

Practice Tags :

Similar Reads