简体   繁体   English

smbj:如何仅获得子目录?

[英]smbj: How can I get subdirectories only?

I use https://github.com/hierynomus/smbj for samba access. 我使用https://github.com/hierynomus/smbj进行samba访问。 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");

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java的目录流仅在目录中而不是其他子目录中获取文件/子目录 - How to use Java's directory stream to get files/subdirectories only within directory not other subdirectories Java:如何仅获取在特定级别存在的子目录名称? - Java: How to get only subdirectories name present at certain level? 如何获取不包括某些子目录的所有子目录? - How to get all subdirectories excluding some subdirectories? 如何使用 Java 中的 glob 来匹配目录和子目录中的文件? - How can I use a glob in Java to match files in directory and subdirectories? SMBJ:如何打印特定子文件夹中存在的所有文件 - SMBJ: How to print all the files present in a particular subfolder 我如何使用 file.listFiles() 来列出子目录和文件 - How can I use file.listFiles() to list subdirectories and files also 如何从数组中获取唯一输入的值? - how can I get the only entered values from an array? 休眠,如何仅根据需要分离和/或获取数据? - Hibernate, How can I detach and/or get data only on demand? 我如何只能从该方法中获取公钥的值 - How can i get only the value of public key from that method 我怎样才能只获得java类的受保护的公共构造函数? - How can I get only protected and public constructors of a java class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM