MMMMS
MMMMS

Reputation: 2201

Java - Copy folder from windows network path

I am trying to copy the folder from the windows network share folder which protected by username and password

String url = "smb://40.123.xx.xx/sharefolder/"; 
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "username", "pas.");
SmbFile dir = new SmbFile(url, auth);
if(dir.exists()) {
File[] directories = new File("\\40.123.xx.xx\\sharefolder")
                .listFiles(file -> file.isDirectory() && file.getName().matches("[0-9]{6}"));

for (File getDirectory : directories) {
Path folder = Paths.get(getDirectory.getAbsolutePath());
                BasicFileAttributes attr = Files.readAttributes(folder, BasicFileAttributes.class);
//          System.out.println("creationTime: " + attr.creationTime());
                System.out.println("creationDate : " + sdf.format(attr.creationTime().toMillis()));
    try {
//      FileUtils.copyDirectoryToDirectory(new File(getDirectory.getAbsolutePath()),new File("F:\\localFolder"));
                    
        } catch (IOException e) {
            e.printStackTrace();
            }
        
    }
}

But the File[] directories return null instead of return all folders path.

How do I access network folder and copy the files to local path.

Upvotes: 1

Views: 451

Answers (1)

DuncG
DuncG

Reputation: 15196

Your UNC pathname has only one escaped backslash at the beginning so is not valid UNC path for the remote filesystem, so you are scanning \40.123.xx.xx\sharefolder when it should be scanning \\40.123.xx.xx\sharefolder:

System.out.println(new File("\\40.123.xx.xx\\sharefolder"))
\40.123.xx.xx\sharefolder        (WRONG)

System.out.println(new File("\\\\40.123.xx.xx\\sharefolder"))
\\40.123.xx.xx\sharefolder       (CORRECT)

Upvotes: 1

Related Questions