Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check directory is existed before creating a new directory in JSCH

How to check the existence of the directory before creating a new directory using JSCH SFTP API? I am trying to use lstat but not sure it's doing the job that I need.Thanks in advance

like image 242
SRy Avatar asked Nov 02 '12 20:11

SRy


1 Answers

This is how I check directory existence in JSch.

Create directory if dir does ont exist

ChannelSftp channelSftp = (ChannelSftp)channel;
String currentDirectory=channelSftp.pwd();
String dir="abc";
SftpATTRS attrs=null;
try {
    attrs = channelSftp.stat(currentDirectory+"/"+dir);
} catch (Exception e) {
    System.out.println(currentDirectory+"/"+dir+" not found");
}

if (attrs != null) {
    System.out.println("Directory exists IsDir="+attrs.isDir());
} else {
    System.out.println("Creating dir "+dir);
    channelSftp.mkdir(dir);
}
like image 130
AabinGunz Avatar answered Sep 27 '22 17:09

AabinGunz