SMBJ: How to print all the files present in a particular subfolder

4.2k views Asked by At

I am facing issue with printing all the file belonging to a particular sub folder in windows VM.

Overview:

I have got a windows VM whose IP address is 10.162.12.12

I want to print all the files name present under C:\MyFolder\MySubFolder

Currently 'MySubFolder' contains 4 cmd files i.e. a.cmd, b.cmd, c.cmd, d.cmd

 try (Connection connection = client.connect("10.162.x.x")) {

        AuthenticationContext ac = new AuthenticationContext("userName", "pwd".toCharArray(), "domainName");
        Session session = connection.authenticate(ac);

        // Connect to Share
        try (DiskShare share = (DiskShare) session.connectShare("MyFolder")) {
            for (FileIdBothDirectoryInformation f : share.list("/MySubFolder")) {
                System.out.println("File : " + f.getFileName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

I am not sure how to pass my "C" drive information and path i.e. where to pass the path.Currently I am getting the following error:

15:48:17.991 INFO c.h.smbj.connection.Connection - Successfully connected to: 10.162.12.12 15:48:18.826 INFO c.h.smbj.connection.Connection - Successfully authenticated userName on 10.162.12.12, session is 140737488355349 15:48:18.826 INFO com.hierynomus.smbj.session.Session - Connecting to \10.162.12.12\MyFolder on session 140737488355349 15:48:19.357 INFO com.hierynomus.smbj.session.Session - Logging off session 140737488355349 from host 10.162.12.12 com.hierynomus.mssmb2.SMBApiException: STATUS_BAD_NETWORK_NAME (0xc00000cc): Could not connect to 10.162.12.12\MyFolder at com.hierynomus.smbj.session.Session.connectTree(Session.java:173) at com.hierynomus.smbj.session.Session.connectShare(Session.java:144) at com.olf.agon.smbj.SMBFile3Trail.main(SMBFile3Trail.java:36)

All i want to know how should I pass the value to my connectionShare() method and to the list() method so that I am able to connect to "\10.162.12.12\C\MyFolder".

1

There are 1 answers

1
Vikas Kumar On BEST ANSWER

I was able to resolve this issue:

SmbConfig smbConfig = SmbConfig
            .builder()
            .withMultiProtocolNegotiate(true)
            .withTransportLayerFactory(new AsyncDirectTcpTransportFactory<>())
            .withSigningRequired(true).build();

    final String SHARE_NAME = "C$";

    final String LOCAL_PATH = "MyFolder/MySubFolder";

    SMBClient client = new SMBClient(smbConfig);

    try (Connection connection = client.connect("10.162.12.12")) {

        AuthenticationContext ac = new AuthenticationContext("userName", "pwd".toCharArray(), "domainName");
        Session session = connection.authenticate(ac);

        // Connect to Share
        try (DiskShare share = (DiskShare) session.connectShare(SHARE_NAME)) {
            for (FileIdBothDirectoryInformation f : share.list(LOCAL_PATH)) {
                System.out.println("File : " + f.getFileName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        client.close();
    }