Last active
January 2, 2016 11:09
-
-
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!
This file contains hidden or 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.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