3

I'd like to be able to rename a list of folders in order to remove unwanted characters (a dot and double space have to become a single space, for example).

Upon clicking a button in the Gui, you'll see a messagebox with the correctly formatted name appear which indicates that both the formatting is correct and the function is called. When I look at the test folders I've created, the names aren't changed (not even after refreshing). Using a hardcoded string doesn't work either.

What am I overlooking?

public void cleanFormat() {
    for (int i = 0; i < directories.size(); i++) {
        File currentDirectory = directories.get(i);
        for (File currentFile : currentDirectory.listFiles()) {
            String formattedName = "";
            formattedName = currentFile.getName().replace(".", " ");
            formattedName = formattedName.replace("  ", " ");
            currentFile.renameTo(new File(formattedName));
            JOptionPane.showMessageDialog(null, formattedName);
        }
    }
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • I think you need to remove old file and create new file. – kosa Dec 06 '12 at 21:17
  • 4
    In the [javadoc](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#renameTo%28java.io.File%29): *Note that the `Files` class defines the `move` method to move or rename a file in a platform independent manner.* – assylias Dec 06 '12 at 21:18
  • I've looked it up on google, and I found the following code several times: `File f = new File("Rename.java~"); f.renameTo(new File("junk.dat"));` There never was anything other than this, are you meaning this is deprecated or something? – Jeroen Vannevel Dec 06 '12 at 21:20
  • @JeroenVannevel It is not deprecated, it is platform dependent and might or might not work depending on various factors. `Files#move` seems more robust according to the docs. – assylias Dec 06 '12 at 21:27
  • @JeroenVannevel You should post the answer as an answer (you are allowed to answer your own question). – assylias Dec 06 '12 at 21:27
  • @assylias: You fixed it, see edit above for the working code. Thanks! – Jeroen Vannevel Dec 06 '12 at 21:28

4 Answers4

7

For future browsers: This was fixed with Assylias' comment. Below you will find the eventual code which fixed it.

public void cleanFormat() {
    for (int i = 0; i < directories.size(); i++) {
        File currentDirectory = directories.get(i);
        for (File currentFile : currentDirectory.listFiles()) {
            String formattedName = "";
            formattedName = currentFile.getName().replace(".", " ");
            formattedName = formattedName.replace("  ", " ");
            Path source = currentFile.toPath();
            try {
                Files.move(source, source.resolveSibling(formattedName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

Well, first of all the File.renameTo is trying to rename a file on the same filesystem.

The following is from java doc

Many aspects of the behavior of this method are inherently platform-dependent: 
The rename operation might not be able to move a file from one filesystem to 
another, it might not be atomic, and it might not succeed if a file with the 
destination abstract pathname already exists.
bhuang3
  • 3,493
  • 2
  • 17
  • 17
0

First of all check return value, File.renameTo returns true if the renaming succeeded; false otherwise. E.g. you cannot rename / move a file from c: to d: on Windows. And most importantly, use Java 7's java.nio.file.Files.move instead.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

The call to getName() returns just the name of the file and not any directory information. So you may be trying to rename the file to a different directory.

Try adding the containing directory to the file object you pass into rename

currentFile.renameTo(new File(currentDirectory, formattedName));

Also like others have said you should be checking the return value of renameTo which is probably false, or use the new methods in Files class which I've found to throw pretty informative IOExceptions.

Michael Krussel
  • 2,586
  • 1
  • 14
  • 16