Results 1 to 3 of 3

Thread: Java class for renaming files for brasero cd-burning

  1. #1
    Join Date
    May 2008
    Beans
    34
    Distro
    Ubuntu 8.04 Hardy Heron

    Java class for renaming files for brasero cd-burning

    hi,

    I've had this glitch with brasero where when I burn mp3 cds brasero burns the files ordered by name and not by album/artist when the file is named in the "01-songname-artist" format. In essence, the actual song order on the cd will be "01-songname-artist", "01-songname-differentartist", "01-songname-thirdartist"..."02-songname-artist", "02-songname-differentartist", "02-songname-thirdartist" and so on and albums that you would want to play consecutively - don't. The solution to this issue was to simply rename the beginning of the file title by adding a prefix composed of aphabetical characters. That way the songs are burned in order of artist/album (as brasero burns according to alphabetical order) i.e. "prefix1-01-songname-artist", "prefix1-02-songname-artist", "prefix1-03-songname-artist"..."prefix2-01-songname-artist", "prefix2-02-songname-artist", "prefix2-03-songname-artist" and so on.

    I wrote a script in Java that automates this for you:

    Code:
    import java.io.Console;
    import java.io.File;
    import java.io.IOException;
    
    public class FileRename {
    
    	/**
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) {
    		if (args.length < 1) {
    			System.err.println("Error: No directory was chosen.");
    			System.exit(0);
    		}
    		File dir = new File(args[0]);
    		File[] listFiles = dir.listFiles();
    		Console c = System.console();
    		int length = 3;
    		if (args.length == 2) {
    			try {
    				length = Integer.parseInt(args[1]);
    			} catch (Exception e) {
    				System.out.println("Invalid length value given. use default? y/n");
    				String ans = c.readLine();
    				c.flush();
    				if (!ans.equals("y")){
    					System.exit(0);
    				}
    			}
    		}
    		while (listFiles[0].isDirectory()) {
    			dir = listFiles[0];
    			listFiles = dir.listFiles();
    		}
    		for (File f : listFiles) {
    			if (f.getName().endsWith("3")) {
    				f.renameTo(new File(f.getParent() + "/"
    						+ dir.getName().substring(0, length) + "_"
    						+ f.getName()));
    			}
    		}
    		
    	}
    
    }
    say, your much needed to be renamed mp3s are in your "/home/<username>/Music/<artist>" directory then all you do is:

    Code:
    java FileRename "/home/<username>/Music/<artist>"
    and your mp3 files are prefixed with the first 3 letters of the <artist>'s folder's name.


    I'd appreciate any sort of feedback on this.

  2. #2
    Join Date
    Jan 2009
    Beans
    58

    Re: Java class for renaming files for brasero cd-burning

    Nice work!
    Could you provide the code in an attachment (FileRename.java)?
    Generic Hashmap for C - Linux isn't Windows - Wikipedia Rocks!
    Great quote: "If you want to do anything interesting, your best strategy is to give up as soon as possible." (MJD)

  3. #3
    Join Date
    May 2008
    Beans
    34
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Java class for renaming files for brasero cd-burning

    Quote Originally Posted by manualqr View Post
    Nice work!
    Could you provide the code in an attachment (FileRename.java)?
    of course, just change the .txt to .java.

    Any ideas about code design/efficiency much appreciated.
    Attached Files Attached Files

Bookmarks

Posting Permissions

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