Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way in java to rename a file , there are about 500 files in a directory

I have 500 pdf files in a directory. I want to remove first five characters of a filename and rename it.

like image 803
Suresh S Avatar asked Apr 27 '10 11:04

Suresh S


4 Answers

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");
    }
}
like image 116
gmhk Avatar answered Nov 13 '22 15:11

gmhk


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
like image 45
Egnatius Avatar answered Nov 13 '22 16:11

Egnatius


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.

like image 4
Stephen C Avatar answered Nov 13 '22 15:11

Stephen C


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");
   }
}
like image 1
Dinushika Rathnayake Avatar answered Nov 13 '22 17:11

Dinushika Rathnayake