I have a folder with a bunch of mp3's which start with the same first letters, so I wan't to get rid of the first parts of their names...
here's some files from the folder:
....
Electroman Metal - heyreivorei.mp3
Electroman Metal - ishvaal.mp3
Electroman Metal - warati.mp3
Electroman Metal - eilum.mp3
Electroman Metal - arbeit.mp3
....
basically, I want to get rid of the "Electroman Metal -" part of their names, however, the code I wrote below only renames the first file in the directory and goes through the rest without renaming them.
I'm really frustrated. Can anyone please tell me what's wrong with this?? I can't seem to find anything that should stop it from renaming other files...
Thanks
Code:/*
* 2011-03-31
*
* 1. read directory
* 3. get keyword to replace from user
* 4. rename all files
* 5. exit
*/
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Mass{
public static void main(String[] args) throws Exception{
new Mass();
}
public Mass() throws Exception{
Scanner input = new Scanner(System.in);
System.out.println("Mass name editor\n");
System.out.print("Enter Directory/path: "); // get the directory of all the files to be renamed
File directory = new File(input.nextLine()); // exit if not found
if (!directory.exists()){
System.out.println("\nDirectory not found!");
System.exit(0);
}
System.out.print("\nEnter the target filname segment to be replaced: "); // get the part of the filename to be removed
String toReplace = input.nextLine();
System.out.print("\nEnter the replacement: "); // get the replacement
String replacement = input.nextLine();
File[] folder = directory.listFiles();
String tempName;
File newFile;
for (File f: folder){
tempName = f.getName();
System.out.println("\nEditing " + tempName + "...");
if (tempName.contains(toReplace)){
tempName = tempName.replace(toReplace, replacement);
newFile = new File(tempName);
f.renameTo(newFile); // this is where the problem is - it only renames the first file!!!
System.out.println("Renamed to " + f.getName());
}
}
System.out.println(folder.length + " files were renamed in the folder: " + directory.getName());
}
}
