JAVA Code to Rename Files in Bulk in a Folder

By | October 23, 2014

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 a TIBCO Consultant By Profession with more than 6 years experience in TIBCO products. He has extensive practical knowledge of TIBCO Business Works, TIBCO Spotfire, TIBCO BE, EMS and TIBCO ActiveSpaces. He has worked on a number of highly critical integration projects in Telecom sector by using his skills in Tibco Designer, Adapters, TIBCO EMS, RV, Administrator, TIBCO BE, TIBCO ActiveSpaces etc. Ajmal Abbasi is also experienced in developing solutions using Oracle PL/Sql, Linux and Java. You can contact Ajmal Abbasi for Consultancy, Technical Assistance and Technical Discussions.

More Posts - Website - Facebook - LinkedIn

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 *