Java Code to Rename Files in Bulk in a Folder

While developing a Java application, you may come across a scenario where you have a large number of files in a folder which don’t have required naming standards or conventions that you are in need of. In such a situation, you will wish to have a piece of code that can rename all the files in the folder in bulk.

In this post, I am going to write a Java program that will rename all the files in a folder based on the given criteria.

For this example scenario; I have a folder D:\files\ which contain a few files having underscore (_) in the names. My requirement is to have all file names without any underscores and having period (.) instead.

Folder contents as of now are shown below:

folder files list

Below is the Java Code that I have written for renaming all the files in this folder by replacing _ with .

package bulkrenamer;
import java.io.File;
/**
 *
 * @author Ajmal Hussain
 */
public class BulkRenamer {
    public String renameBulk (String folderName, String ReplaceFrom, String ReplaceWith)
    {
        try
        {
        // This method will rename all files in a folder by chaning ReplaceFrom string with ReplaceWith string
        File folder=new File(folderName);
        File[] filesList=folder.listFiles();
        for (int i=0; i< filesList.length; i++)
        {
            String newName= (filesList[i].toString().replaceAll(ReplaceFrom, ReplaceWith));
            
            filesList[i].renameTo(new File(newName));
            
            
        }
        return "Successfully renamed "+filesList.length+" files.";
        }
        catch (Exception e)
        {
            return (e.getMessage());
            
        }
                
    }

 
    public static void main(String[] args) {
        BulkRenamer rn=new BulkRenamer();
      String result=  rn.renameBulk("D:\\files\\", "_", ".");
      System.out.println(result);
      
        
    }
}

The above Java Class has one public method that is taking a folder name and two other strings as input. It uses second string as a search criteria and replaces it with the third string (in this case second string is _ and third string is .).

After executing the above code in Netbeans IDE, I got below result stating that all the four files in the given directory have been renamed successfully:

java successfully run file rename code

Now, If we go to the folder (D:\files\), we can see that all the four files have been renamed by replacing _ with . as shown in below screenshot:

java files renamed

 

Ajmal Abbasi

Ajmal Hussain Abbasi is Integration Consultant By Profession with 13+ years experience in Integration domain mainly with TIBCO products. He has extensive practical knowledge of TIBCO Business Works, TIBCO Cloud, TIBCO Flogo, TIBCO Mashery, TIBCO Spotfire, EMS and TIBCO ActiveSpaces. He has worked on a number of highly critical integration projects in various sectors by using his skills in TIBCO Flogo, TIBCO API Management (Mashery), TCI, Tibco Designer, TIBCO Business Studio, Adapters, TIBCO EMS, RV, Administrator, TIBCO BE, TIBCO ActiveSpaces etc. Ajmal Abbasi has experience with MuleSoft ESB as well. Ajmal Abbasi is also experienced in the area of API Management particularly with WSO2 API management platforms. Ajmal Abbasi is also experienced in developing solutions using Core Java and J2EE Technologies. You can contact Ajmal Abbasi for Consultancy, Technical Assistance and Technical Discussions.

More Posts - Website - Facebook - LinkedIn - YouTube

One thought on “Java Code to Rename Files in Bulk in a Folder

  1. Reddy

    How to process 5GB xml file in Tibco BW and update in to database.

    plz explain by using java code and with out java code also.

    Thanks,
    Reddy

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *