Search code examples
javagroovysmb

JCIFS SmbFile RenameTo fails to a move file


I have the strangest behavior using the JCIFS SmbFile.renameTo() method. When I execute the code below it should move the network file from test1 to test2, but instead it creates a folder in test2 called test.xml and throws the following error "Cannot create a file when that file already exists..." I can't figure it out. Why is this method doing this?

 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication (sDomain, 
                                       sUsername, sPassword);
 SmbFile smbFromFile = new SmbFile("smb://test1/test.xml", auth);
 SmbFile smbToFile = new SmbFile("smb://test2/test.xml", auth);
 smbFromFile.renameTo(smbToFile);

Solution

  • There's an interesting difference between copyTo(SmbFile) and renameTo(SmbFile) - only one of them says This file and the destination file do not need to be on the same host. As renameTo(SmbFile) does not say that, I can only assume you should use copyTo and then delete() the original.

    SmbFile smbFromFile = new SmbFile("smb://test1/test.xml", auth);
    SmbFile smbToFile = new SmbFile("smb://test2/test.xml", auth);
    // smbFromFile.renameTo(smbToFile);
    smbFromFile.copyTo(smbToFile);
    smbFromFile.delete();