3 Replies Latest reply: May 15, 2010 10:36 PM by 3004 RSS

    Mass removing parts of a Filename

    843789
      Okay so I have around 500 pictures from my friend when we went on vacation (shes got a thing for pictures). Anyway I want to change what the files are called there like this 1_76573.jpeg, 2_94423.jpeg I want that to be just the 1.jpeg 2.jpeg and so on. Also the numbers I showed there are random because they all are. Is this at all possible or should I look at another programming language?

      Thanks
        • 1. Re: Mass removing parts of a Filename
          843789
          It is quite possible.

          Look at the [File class|http://java.sun.com/javase/6/docs/api/java/io/File.html] for ways of getting the names of files in a particular directory and for renaming files. The String class has methods that may help with the transformation of the file name.

          I say "may" help because you haven't really said what you want the transformation to be. As posted your description sounds like you might end up with just 9 files (1.jpeg through 9.jpeg). But if, say, the file names currently have a unique prefix (the bit before the underscore), then the String class has methods for extracting that prefix and the File class will allow you to remname the files.

          If you don't care about the relationship between the old file names and the new ones the File class alone will give you all you need: just rename them in a loop.
          • 2. Re: Mass removing parts of a Filename
            843789
            Hi,
            I think something like this will work
            package sunforums;
            
            import java.io.File;
            
            class Renamer
            {
            
                private static final String fileExtension = ".jpeg";
                private static int counter = 1;
            
                protected Renamer()
                {
                }
            
                protected void rename(String pathname)
                {
                    File file = new File(pathname);
                    for (File f : file.listFiles()) {
                        if (f.canRead() && f.canWrite() && f.getName().endsWith(fileExtension)) {
                            File oldFile = f;
                            if (f.renameTo(new File(String.valueOf(counter) + fileExtension))) {
                                System.out.println(oldFile.toString() + " renamed to " + f.toString());
                                counter++;
                            } else
                                System.err.println("Couldn't rename " + f.toString());
                        }
                    }
                }
            
                public static void main(String[] args)
                {
                    Renamer r = new Renamer();
                    r.rename(args[0]);
                }
            
            }
            Good luck

            Edited by: Oclamora on May 15, 2010 7:57 PM
            edited counter initialization
            • 3. Re: Mass removing parts of a Filename
              3004
              Oclamora wrote:
              Hi,
              I think something like this will work
              Stop giving out full solutions. It does NOT help the OP.