Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move (not copy) a file with JCIFS?

Tags:

smb

jcifs

I'm wondering how I can move a file from one folder to another on an SMB share, using JCIFS.

First, there is no move() method whatsoever.

Then, this approach:

SmbFile smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
SmbFile smbToFile = new SmbFile("smb://...pool/to-here/the-file.pdf", auth);
smbFromFile.renameTo(smbToFile);

throws an Exception, "The system cannot find the path specified."

Rename only works in the same path. Altering the parameters doesn't help.

Right now, I'm using

smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
smbToFile = new SmbFile("smb://...pool/to-here", auth);
smbFromFile.copyTo(smbToFile);
smbFromFile.delete();

This feels somehow wrong.

Unfortunately, in the docu I don't find anything about moving a file.

Does somebody have a bit more information? It should be a part of SMB, right (SMB_COM_MOVE)?

like image 324
peter_the_oak Avatar asked Dec 15 '22 10:12

peter_the_oak


2 Answers

Turned out I was a muppet as I had messed up my configuration parameters.

Both ways are working fine:

Method 1:

SmbFile smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
SmbFile smbToFile = new SmbFile("smb://...pool/to-here/the-file.pdf", auth);
smbFromFile.renameTo(smbToFile); 

Method 2:

smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
smbToFile = new SmbFile("smb://...pool/to-here/the-file.pdf", auth);
smbFromFile.copyTo(smbToFile);
smbFromFile.delete();
like image 87
peter_the_oak Avatar answered Feb 11 '23 03:02

peter_the_oak


There are two possible Scenarios:

1.) The file needs to be moved on the same server ( That is, the authentication details for Input folder and Output folder are same).

Use renameTo() method.

 public boolean moveFile(SmbFile file) {
    log.info("{"Started Archiving or Moving the file");
    String targetFilePath = this.archiveDir + file.getName(); //Path where we need to move that file.
    try {
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", userId, userPassword);
        log.info("targetFilePath: {} , currentFile : {}",targetFilePath, file);
        SmbFile targetFile = new SmbFile(targetFilePath, auth); 
          //authenticate the SmbFile
        try {
            file.renameTo(targetFile); //Use renameTo method for same server
            log.info("Archived File : {} to: {}", file.getName(), 
            targetFile.getName());
            return true;
        } catch (SmbException e) {
            log.error("Unable to Archive File: {}", file.getName());
            return false;
        }
    } catch (MalformedURLException e) {
        log.error("Connection failed to Server Drive: {}", targetFilePath);
    }
    return false;
}

2.) The file needs to be moved on Different server ( That is, the authentication details for Input folder and Output folder are NOT same).

Use copyTo() method.

Here I will suggest, You can firstly authenticate the first server on which file is present, And check if the File exists, If it is existing then add that in a list :

public List<SmbFile> xmlFiles = new ArrayList<>(); //Here we will add all the files which are existing.

public boolean isFileExists() throws MalformedURLException, SmbException {
  NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", 
  userID, userPassword); //authenticating input folder.
    SmbFile smbFile = new SmbFile(inputFolder, auth);
    SmbFile[] smbFiles = smbFile.listFiles();
    boolean isFilePresent = false;
    if (smbFiles.length > 0) {
        for (SmbFile file : smbFiles) {
            if (file.getName().toLowerCase(Locale.ENGLISH)              
       .contains(AppConstant.FILE_NAME.toLowerCase(Locale.ENGLISH))) {
                xmlFiles.add(file);
                isFilePresent = true;
            }
        }
    }
    if (isPlanFilePresent) {
        log.info("Number of files present on Server: {}",smbFiles.length);
        return true;
    }
    return false;
}

This will give you the files in the list. Go ahead to copy it to another server. Note that you need to authenticate here for the output Folder only.

 public boolean moveFile(SmbFile file) {
    log.info("Started Moving or Archiving the file");
    String toFilePath = this.outputFolder + file.getName(); //path where you need to copy the file from input folder.
    try {
        NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication("", outputFolderUserId, outputFolderPassword); //authenticating output folder
        log.info("targetFilePath: {} and currentFile : {}", toFilePath, file);
        SmbFile targetFile = new SmbFile(toFilePath, auth1);
      
        try {
            file.copyTo(targetFile);
            file.delete(); //delete the file which we copied at our desired server
            log.info("Archived File : {} to: {}", file.getName(), targetFile.getName());
            return true;
        } catch (SmbException e) {
            log.error("Unable to Archive File: {}", file.getName());
            return false;
        }

    } catch (MalformedURLException e) {
        log.error("Connection failed to Server Drive: {}", toFilePath);
    }
    return false;
}
like image 23
Kapil Pateriya Avatar answered Feb 11 '23 05:02

Kapil Pateriya