6

I would like to iterate over files that are in network UNC path so that I can operate over them, is it possible?

I'm trying in below way(see the below code) and it's not listing the files.

But, with windows explorer I can access that folder and I can see, modify even remove them.

// created with this command: mklink /D C:\Users\user\Desktop\repo \\serverIp\public\repo
File repo = new File("C:\\Users\\user\\Desktop\\repo"); // symlink

final Path dir = FileSystems.getDefault().getPath(repo.getCanonicalPath());

Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path newDir, BasicFileAttributes attrs) throws IOException {
        System.out.println(newDir.toAbsolutePath());                
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        System.out.println(file.toAbsolutePath());
        return FileVisitResult.CONTINUE;
    }
});

Did I create the symbolic link correctly?

Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
Emrah Mehmedov
  • 1,492
  • 13
  • 28

1 Answers1

5

I think you need to use FOLLOW_LINKS option.

import static java.nio.file.FileVisitResult.*;
Files.walkFileTree(dir, EnumSet.of(FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { ... ))
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • Not working if the root directory is symbolic link... maybe it will work if i put in another folder and then as a subfolder to that root folder it will follow the symlink but what i need is not this way – Emrah Mehmedov Feb 10 '17 at 13:31
  • @EmrahMehmedov Can you please check if your link exists? something like `Files.isSymbolicLink(Paths.get())` and `Files.exists(Paths.get(..))` – Anton Balaniuc Feb 11 '17 at 20:34
  • For me it worked being the root directory a symbolic link. – viniciussss Jul 19 '17 at 00:51