Results 1 to 4 of 4
  1. #1
    themulator is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default 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());
    	}	
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  3. #3
    themulator is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    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....:(

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Windows often locks files. Certainly if the .mp3 is playing you won't be able to rename it. Also there could be problems with permissions.

Similar Threads

  1. need help with package renaming
    By Madz in forum New To Java
    Replies: 2
    Last Post: 11-25-2009, 10:39 AM
  2. Renaming underlying directory in Java project
    By Rodrigo Braz in forum Eclipse
    Replies: 3
    Last Post: 03-08-2009, 08:25 AM
  3. Renaming a method/variable
    By gapper in forum Eclipse
    Replies: 0
    Last Post: 01-31-2008, 02:29 PM
  4. Renaming a class
    By mew in forum Eclipse
    Replies: 2
    Last Post: 12-07-2007, 12:29 AM
  5. Renaming a class in Eclipse
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-04-2007, 11:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •