javasambasmbsmbj

smbj: How can I get subdirectories only?


I use https://github.com/hierynomus/smbj for samba access. I want get only the subfolders of my target directory. Whith following code I get "..", "." and all the files - is there an elegant way to get subdirectories only?

SmbConfig config = SmbConfig.builder().withMultiProtocolNegotiate(true).build();
    smbClient = new SMBClient(config);
    Connection connection = smbClient.connect(smbServerName);
    AuthenticationContext ac = new AuthenticationContext(smbUser, smbPassword.toCharArray(), smbDomain);
    Session session = connection.authenticate(ac);
    share =  (DiskShare) session.connectShare(smbShareName);    
    List<FileIdBothDirectoryInformation> subs = share.list("mydirWhichSubDirsIwant");

Solution

  • You need to filter out the results from the returned list. If you only want the subdirectories you can get them as follows:

    List<String> subDirectories = new ArrayList<>();
    for (FileIdBothDirectoryInformation sub: subs) {
        String filename = sub.getFileName();
        if (".".equals(filename) || "..".equals(filename)) {
            continue;
        }
        if (EnumWithValue.EnumUtils.isSet(sub.getFileAttributes(), FileAttributes.FILE_ATTRIBUTE_DIRECTORY)) {
            subDirectories.add(filename);
        }
    }