Hello everyone
I have created a java program using jsch0.1.50.jar to remove the directories in the sftp server. But I could not remove the folder "03-03-2014" as it contains a subfolder named "1837hrs" when I try to run the program I am getting the following error:

4: The directory is not empty.
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2833)
at com.jcraft.jsch.ChannelSftp.rmdir(ChannelSftp.java:2109)
at SFTPrmdirinJava.main(SFTPrmdirinJava.java:40)
So how do I remove all the directories and the subdirectories all at once, is this possible ? Please check my code:

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SFTPrmdirinJava {        
    /**
    * @param args
    */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        String SFTPHOST = "localhost";
        int    SFTPPORT = 22;
        String SFTPUSER = "Administrator";
        String SFTPPASS = "Password Changed";
        String SFTPWORKINGDIR = "SCPDemo/";
        String Dirname = "03-03-2014";                 
        Session     session     = null;
        Channel     channel     = null;
        ChannelSftp channelSftp = null;         
        try {           
            JSch jsch = new JSch();
            session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
            session.setPassword(SFTPPASS);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(SFTPWORKINGDIR);                         
            channelSftp.rmdir(Dirname);                 
            System.out.println("The directory "+Dirname+" removed successfully");
            session.disconnect();           
            channel.disconnect();                 
        }catch(Exception ex){
           ex.printStackTrace();
        }
    }
}      

PS: I am using windows server 2003 and using Bitwise ssh server which runs on port no 22.

traverse the directory and remove all sub directories, and all sub directories of the sub directories, etc, a recursive method will suffice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.