Skip to content

Instantly share code, notes, and snippets.

@francis36012
Last active December 26, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francis36012/7178075 to your computer and use it in GitHub Desktop.
Save francis36012/7178075 to your computer and use it in GitHub Desktop.
import java.io.*;
public class BatchRename {
private static File userfile;
private static void renameAll(File folder) throws Exception{
String currentname;
String newname;
File newfile;
for (File fileEntry : folder.listFiles()) {
// Recursively go through directories
if (fileEntry.isDirectory()) {
renameAll(fileEntry);
}
else {
currentname = fileEntry.getName();
newname = currentname.replaceAll(" ", "-");
// Check to see if file needs to be renamed
if (!(newname.equals(currentname))) {
newfile = new File(newname);
// Report progress
if (fileEntry.renameTo(newfile)) {
System.out.println("Rename of " + fileEntry.getName() + " successful");
}
else {
System.out.println("Error renaming " + fileEntry.getName() + " " + newname + "");
}
}
}
}
}
public static void main(String[] args) throws Exception{
String userfile_str;
if (args.length == 1) {
userfile_str = args[0];
userfile = new File(userfile_str);
renameAll(userfile);
}
else {
System.out.println("Usage: java BatchRename <directory path>");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment