0

I tried googling everywhere, but cannot find anywhere. I am trying to accomplish is to go to SFTP or SSH and delete/remove the directory.

here is my code. any help is appreciated. this code does not delete and remove the directory as it suppose to do it.

public static boolean removeDirectory(String path, Session session) throws InterruptedException {

        ChannelExec channelExec = null;
        try {
            channelExec = (ChannelExec) session.openChannel("exec");

            String command = "rm -rf "+path;
            channelExec.setCommand(command);


            channelExec.connect();
            Thread.sleep(5000); 
            channelExec.disconnect();
        } catch (JSchException e1) {
            return false;
        }               
        return true;
    }
Kenster
  • 23,465
  • 21
  • 80
  • 106
jimagic
  • 4,045
  • 2
  • 28
  • 49
  • How many files are contained within the remote directory? Is it possible the command isn't completing in the five seconds that you wait for it? Are you getting an exception when you run this? What is the message in the exception? – Kenster Oct 24 '14 at 15:59
  • no exception is thrown and when i try to do command --> rm -rf (directory_name) it does within 1 seconds. – jimagic Oct 24 '14 at 16:05

1 Answers1

0

I finally got the solution. I have used jScape library to delete the directory recursively

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 SFTPExample extends SftpAdapter {
     static String hostName = "hostname";
     static String username = "username";
     static String password = "password";;
     static String directory = "directory";;
    private static Sftp sftp;

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

    @SuppressWarnings("unchecked")
    public static boolean deleteDir(List <String> path) throws SftpException {
        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(" ------ File Name: " + eachOne);
                System.out.println(directory+eachOne);
                sftp.deleteDir(directory+eachOne, true);
            }
        }

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

        return true;
    }

    // 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();
    }
}
jimagic
  • 4,045
  • 2
  • 28
  • 49