[Java] Renaming a batch of files

Due to some reason, the file names in my MP3 folder begin with two digits like this: 01 file.mp3. It is too tedious to rename them one by one since there are too many (hundreds of them). So I wrote a small program to remove the digits :) .



import java.util.*;
import java.io.*;

public class FileTest
{
  public static void main(String[] args)
  {
    // Create a directory
    File dir = new File("/path/to/MP3/");
    // get the file names with path
    File[] filePath = dir.listFiles();
    for(File fp :filePath)
    {
        // get the file name from the path
        String filename = fp.getName();

        // String newname = filename.substring(3);
        File newfile = new File("/path/to/MP3/"+newname);
        fp.renameTo(newfile);
    }
  }
}

No comments: