jimagic
jimagic

Reputation: 4065

SFTP non-empty folder delete recursively

I tried JSCH for deleting non empty folder via SFTP, but it did not work.

This one works, but it is not free :( I am already past 30 days trial period. My company refuse to pay them for just deleting the folder via SFTP.

package com.test.cvsutil;

import java.util.List;

import org.apache.log4j.Logger;

import com.jscape.inet.sftp.Sftp;
import com.jscape.inet.sftp.SftpException;
import com.jscape.inet.sftp.events.SftpAdapter;
import com.jscape.inet.ssh.util.SshParameters;

public class SFTPFileDeleter extends SftpAdapter {
     static String hostName = ServerProperties.getProperty("test.ftp.hostname");
     static String username = ServerProperties.getProperty("test.ftp.login");
     static String password = ServerProperties.getProperty("test.ftp.password");
     static String directory = ServerProperties.getProperty("test.ftp.directory");
    private static Sftp sftp;

    private static org.apache.log4j.Logger log = Logger.getLogger(SFTPFileDeleter.class);


    public static boolean deleteDir(List <String> path) throws SftpException {
        Boolean flag = false;
        log.info("------------------------ file(s) delete started ------------------------");
        sftp = new Sftp(new SshParameters(hostName, username, password));

        sftp.connect();
        sftp.setDir(directory);

        for (String eachOne : path) {
            if (!sftp.getDirListingAsString(eachOne).equals("")){
                log.info(" ------  Deleted Folder/Scenario Name: " + eachOne);
                //log.info(" ------  check file path: " + directory+eachOne);
                //System.out.println(directory+eachOne);
                sftp.deleteDir(directory+eachOne, true);
                flag = true;
            }
        }

        sftp.disconnect();
        log.info("------------------------ file(s) delete finished -----------------------");

        return flag;
    }

    // open connection to the remote server.
    public static void openConnection() throws SftpException {
        sftp.connect();
    }

    // disconnect from the remote server.
    public static void closeConnection() {
        sftp.disconnect();
    }
}

and this is the error I got, so jscape sftp will not work for me.

2014-11-29 09:00:04 ERROR MainEntry:47 - There was error and message is java.lang.RuntimeException: Your Secure FTP Factory license expired on Sun Nov 23 00:31:21 CST 2014. Please v
isit http://www.jscape.com to purchase a licensed copy.
2014-11-29 09:00:04 ERROR MainEntry:48 - There was error and stack-trace is [Ljava.lang.StackTraceElement;@fdb00d

any help is much appreciated. Asking here if anybody know other libraries that does delete non-empty folder.

Upvotes: 0

Views: 4869

Answers (1)

jimagic
jimagic

Reputation: 4065

I was able to delete the directory recursively. Thanks @Rob

here is the code that will help if anybody looking for it out there.

public static void deleteDirectory(ChannelSftp sftp, String oldestBackup) throws SftpException {
    if (isDir(sftp, oldestBackup)) {
        sftp.cd(oldestBackup);
        Vector < LsEntry > entries = sftp.ls(".");
        for (LsEntry entry: entries) {
            deleteDirectory(sftp, entry.getFilename());
        }
        sftp.cd("..");
        sftp.rmdir(oldestBackup);
    } else {
        sftp.rm(oldestBackup);
    }
}

private static boolean isDir(ChannelSftp sftp, String entry) throws SftpException {
    return sftp.stat(entry).isDir();
}

Upvotes: 8

Related Questions