8

Is it possible with FTPClient (Apache commons-net) to check if a remote directory exists?

I want to do something like this:

ftp.isDirectory(String path) //returns true, false

And then get permissions (chmod) of directory:

ftp.getPermisions(String path) //returns -rwxr-xr-x 
skaffman
  • 398,947
  • 96
  • 818
  • 769
Xerg
  • 293
  • 2
  • 4
  • 10

5 Answers5

16

Try to change the working directory to the directory you need to check:

boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir")
TechZen
  • 64,370
  • 15
  • 118
  • 145
Juris
  • 169
  • 1
  • 3
  • 1
    This seems to be the only way to really do this, unfortunately. I think it's just more the limitations of FTP than the client. – MaddHacker May 17 '12 at 14:55
6

What you just Need to check is just ftpClient.cwd("your Directory Name")

this will return you integer values

250 - If File Exists

OR

550 - If File Doesn't Exist

For Example,

if(ftpClient.cwd(uploadDirectoryPath)==550){
     System.out.println("Directory Doesn't Exists");
}else if(ftpClient.cwd(uploadDirectoryPath)==250){
     System.out.println("Directory Exists");
}else{
     System.out.println("Unknown Status");
}
Shalin Patel
  • 1,079
  • 1
  • 10
  • 15
  • 1
    The CWD command means - Change Working Directory btw, so its not really different from boolean directoryExists = FTPClient.changeWorkingDirectory("path/to/somedir") – Javo Jun 15 '18 at 11:56
  • Yes, it is same but I gave this example as there is some other status also which we can handle. And we can have more control over our coding that's why. – Shalin Patel Jun 16 '18 at 20:32
  • 1
    If you send CWD, you will lost last working directory. Sometimes it would be the problem when handle multi-thread situation. – Aura Aug 02 '19 at 14:50
  • @Aura yes exactly, happened with me I don't want to change last working directory. – Malav Mevada Aug 29 '22 at 09:51
1

I needed to figure this out too, but after doing a little play, I think I figured it out. I havent gotten to test this yet, but I think it will do the trik

FTPFile file[];
file = new FTPFile[ftpClient.listFiles().length];
for (int i = 0; i<file.length; i++) {
if (file[i].getName() == "directory name") {
    if (file[i].isDirectory()) {
    //Do stuff if it is a directory here
         if (file[i].hasPermission(access, permission) {
        //Do whatever you want with permissions - access and permission arguments are int's
                        }
    }
}
}

Hope this works/helps. This also seems like a pretty redundant way, so there may be a better way of doing it. Idk, im new to this library and android

Shaun
  • 5,483
  • 10
  • 40
  • 49
0

If the remote host supports it, the simplest method is mlistFile().

if (ftpClient.featureValue("MLST") != null) {
    FTPFile file = ftpClient.mlistFile(null);
    boolean b = file.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION);
}
jaco0646
  • 15,303
  • 7
  • 59
  • 83
0

Here is my take on to find whether given path represents a directory or a file.

public static boolean isFtpPathDirectory(String file_path)
{
        try (InputStream inputStream=ftpClient.retrieveFileStream(file_path))
        {
            return inputStream == null;
        } catch (IOException e) {
            return false;
        }


}
shankar_vl
  • 72
  • 6