0

I try to rename a whole directory programmatically. The directory is on a server that is mounted on the local file system. I'm trying it to do like this:

public static void main(String[] args) {
    File dir = new File("/Volumes/video/Serien/Scrubs/Season 1");
    System.out.println("Start renaming: " + dir);

    String[] files = dir.list();
    for (String file : files) {
        System.out.println("Old name: " + file);
        File renamedFile = new File(file);
        System.out.println(renamedFile.toString());
        boolean success = renamedFile.renameTo(new File("Test " + renamedFile.toString()));
        System.out.println("New name: "+ renamedFile.toString());
        System.out.println(success);
        break;
    }
}

I now that it tries only to rename the first one, but nevertheless it returns false and doesn't rename.

So any hints why? I do not get any exceptions. I think it is because the server requires authentication?

Edit: Since renameTo() seems to be platform-dependent: I'm using Lion OSX

  • Why exactly should it succeed if you don’t actually rename anything? Change the name and it might actually succeed. – Bombe Sep 03 '11 at 22:39
  • I should have clarified this. It also does not success if I change the name. –  Sep 03 '11 at 22:45
  • There seem to be a couple of interesting answers in http://stackoverflow.com/questions/1000183/reliable-file-renameto-alternative-on-windows – Arnout Engelen Sep 03 '11 at 22:50
  • Thanks I'll check it. I'll also add some OS information to my question. I'm using Lions OSX –  Sep 03 '11 at 22:52

1 Answers1

2

Try using a fullpath + the directory name when you are trying to rename for both old and renamed directory. I believe list() returns the directory name only without the fullpath. I had similar problem before and it worked when I did that. Hopefully that works for you as well.

momo
  • 21,233
  • 8
  • 39
  • 38