Last active
December 26, 2015 16:09
-
-
Save francis36012/7178075 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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