Results 1 to 4 of 4
Thread: File renaming help
- 03-31-2011, 07:20 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
File renaming help
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
Java 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()); } }
- 03-31-2011, 09:23 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
the code I wrote below only renames the first file in the directory and goes through the rest without renaming them.
Are the files in fact (ie in the actual file system, independent of what your code reports) not being renamed? I ask because you have this code:
Java Code:f.renameTo(newFile); System.out.println("Renamed to " + f.getName());
But renameTo() does not change what getName() returns.
It might be a good idea to say what is actually output by your code. Ie indeicate how many times you go round the for loop. Also The API indicates that you should check the boolean return value of renameTo(), so do that and output the result.
The SYstem.out.println() at the end is bogus: you are reporting the number of files in a folder, not the the number that were renamed. If you want to report the number that were renamed actually count them within the for loop and report that (correct) result.
- 03-31-2011, 09:27 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 15
- Rep Power
- 0
Thank's for your reply.
the boolean value returns false. I don't really understand why it would return false. The getName() method returns the new names properly, but the file is never actually renamed....:(
- 04-01-2011, 05:24 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
Similar Threads
-
need help with package renaming
By Madz in forum New To JavaReplies: 2Last Post: 11-25-2009, 09:39 AM -
Renaming underlying directory in Java project
By Rodrigo Braz in forum EclipseReplies: 3Last Post: 03-08-2009, 07:25 AM -
Renaming a method/variable
By gapper in forum EclipseReplies: 0Last Post: 01-31-2008, 01:29 PM -
Renaming a class
By mew in forum EclipseReplies: 2Last Post: 12-06-2007, 11:29 PM -
Renaming a class in Eclipse
By Java Tip in forum Java TipReplies: 0Last Post: 12-04-2007, 10:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks