I have 500 pdf files in a directory. I want to remove first five characters of a filename and rename it.
Sample code for you to rename the List of files in a given directory. In the below example, c:\Projects\sample is the folder, the files which are listed under that have been renamed to 0.txt, 1.txt, 2.txt, etc.
I hope this will solve your problem
import java.io.File;
import java.io.IOException;
public class FileOps {
    public static void main(String[] argv) throws IOException {
        File folder = new File("\\Projects\\sample");
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                File f = new File("c:\\Projects\\sample\\"+listOfFiles[i].getName()); 
                f.renameTo(new File("c:\\Projects\\sample\\"+i+".txt"));
            }
        }
        System.out.println("conversion is done");
    }
}
something like this should do (Windows version):
import java.io.*;
public class RenameFile {
    public static void main(String[] args) {
        // change file names in 'Directory':
        String absolutePath = "C:\\Dropbox\\java\\Directory";
        File dir = new File(absolutePath);
        File[] filesInDir = dir.listFiles();
        int i = 0;
        for(File file:filesInDir) {
            i++;
            String name = file.getName();
            String newName = "my_file_" + i + ".pdf";
            String newPath = absolutePath + "\\" + newName;
            file.renameTo(new File(newPath));
            System.out.println(name + " changed to " + newName);
        }
    } // close main()
} // close class
Use File.listFiles(...) to list the files in the directory, String.substring(...) to form the new file names, and File.rename(...) to do the renaming.  
But I suggest that you have your application check that it can rename all of the files without any collisions before you start the renaming.
But @Pascal's comment is spot on. Java is not the simplest tool for doing this kind of thing.
As in your requirement, using java can achieve this like follows,
//path to folder location
String filePath = "C:\\CMS\\TEST";
//create direcotory TEST
File fileDir = new File(filePath);
if (!fileDir.exists()) {
   fileDir.mkdirs();
}
//create two file named 12345MyFile1.pdf, 12345MyFile2.pdf
File file1 = new File(filePath, "\\12345MyFile1.pdf");
file1.createNewFile();
File file2 = new File(filePath, "\\12345MyFile2.pdf");
file2.createNewFile();
//rename the file
File file = new File(filePath);
String[] fileList = file.list();//get file list in the folder
for (String existFileName : fileList) {
   String newFileName = existFileName.substring(5);//remove first 5 characters
   File sourceFile = new File(filePath + File.separator + existFileName);
   File destFile = new File(filePath + File.separator + newFileName);
   if (sourceFile.renameTo(destFile)) {
       System.out.println("File renamed successfully from: " + existFileName + " to: " + newFileName);
   } else {
       System.out.println("Failed to rename file");
   }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With