Tuesday, March 20, 2012

Java: File Renaming

In a project, sometimes you get a bunch of files that need to be renamed. They either have same prefix/suffix and it would be a pain to rename them one by one. Being a beginner programmer, I felt obliged to build a quick "script" instead of download an app. So here's the code:

import java.io.File;
import java.util.*;
import java.io.IOException;

class Renamer {
 public static void main(String[] args) throws Exception {
  String path = ".";

  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles();

  for (File f: listOfFiles) {
   f.renameTo(new File(f.getName().replaceAll("_mdpi","")));
  }
 }

}

Using Textpad's ctrl+1 and ctrl+2 shortcuts for run and compile, the task becomes easy., but I'm sure that using one liners in other script proggies are faster. I need get around to learning Python or something. Hmmmm....

No comments:

Post a Comment