Saturday, December 26, 2020

JAVA Code to Rename all files in a folder

JAVA Code to Rename all files in a folder


import java.io.*;

public class RenameFileNames 

{

     public static void main(String[] args) 

     {

        String absolutePath = "E:\\Wllpapers\\unbelive";

        File dir = new File(absolutePath);

        File[] filesInDir = dir.listFiles();

        int i = 0;

        for(File file:filesInDir) {

            i++;

            String name = file.getName();

            String newName = "pic" + i + ".jpeg";

            String newPath = absolutePath + "\\" + newName;

            file.renameTo(new File(newPath));

            System.out.println(name + " changed to " + newName);

        }

    }

}