Skip to content

Instantly share code, notes, and snippets.

@powerlim2
Last active January 2, 2016 11:09
Show Gist options
  • Save powerlim2/8294772 to your computer and use it in GitHub Desktop.
Save powerlim2/8294772 to your computer and use it in GitHub Desktop.
'Java code' to add prefix to all files in a specified directory. All you need is to modify two variables. That's it!
import java.io.File;
/**
* Created with IntelliJ IDEA.
* User: Joonhyung
* Date: 1/6/14
* Time: 10:20 PM
* To change this template use File | Settings | File Templates.
*/
public class AddPrefixToFiles {
public static void main(String[] args) throws Exception {
// VARIABLE SET UP.
String directory = "PATH-TO-DIRECTORY HERE!";
String PREFIX = "PREFIX HERE!";
// No need to modify the code below.
File[] files = new File(directory).listFiles();
for(File file : files){
if(file.isFile()){
String ORIGINALNAME = file.getName();
File NEWFILE = new File(directory+PREFIX+" "+ORIGINALNAME);
boolean success = file.renameTo(NEWFILE);
if (!success) {
System.err.println("FAILED to rename "+file.getName());
} else {
System.out.println(NEWFILE.getName());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment