0

I wrote this function below to find all folders named 'Construction Documents', I want it to copy all its contents over to another directory.

        private void findDirectory(File parentDirectory, String insertDir) {
            if(foundFolder) {
                return;
            }
            File[] files = parentDirectory.listFiles();
            for (File file : files) {
                if (file.isFile()) {
                    continue;
                }
                if (file.getName().equals("Construction Documents")) {
                    System.out.println(file.getAbsolutePath());
                    if(file.getParent().contains("Original") || file.getParent().contains("Revision") || file.getParent().contains("R0")){
                        new File(insertDir + file.getParent()).mkdir();
                        try {
                    //      FileUtils.copyDirectory(new File(file), new File(insertDir + file.getParent().toString()));
                            Files.copy(file.toPath(), new File(insertDir + file.getParent().toString()).toPath());
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                if(file.isDirectory()) {
                    findDirectory(file, insertDir);
                }
            }
        }

insertDir is the String of the file path id like to insert the folder in. I tried using FileUtils, however this copied the ENTIRE directory over, including the many parent files before the actual construction documents folder. Native java didnt work either, and simply led to errors.

In short, I would like to copy a single folder, with all its subfolders and contents over to another folder, however without all the parent files from the copied file being created on the destination end.

ZKayyali
  • 23
  • 8
  • I don't think `continue` is doing what you think it is. It is actually going to the next `File` in the loop, not continuing with your code. – Dan W Jul 29 '16 at 18:12
  • That part actually works fine. That part is just ignoring files while its searching for the proper folder to copy over. – ZKayyali Jul 29 '16 at 18:14

1 Answers1

1

Something along those lines:

Path srcDir = Paths.get("c:\\src");
Path dstDir = Paths.get("c:\\dst");
Files.walkFileTree(srcDir, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Path dstSubDir = dstDir.resolve(srcDir.relativize(dir));
        Files.createDirectories(dstSubDir);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Path dstFile = dstDir.resolve(srcDir.relativize(file));
        Files.copy(file, dstFile);
        return FileVisitResult.CONTINUE;
    }
});
Jazzwave06
  • 1,883
  • 1
  • 11
  • 19
  • This still copied all the parent directories from the network root. I would like to copy just the final folder selected, with all its sub-folders and files. – ZKayyali Jul 29 '16 at 18:51
  • This copies a directory into another directory. There's no parent directory involved. If you need to select one of the subdirectory, prepend the `preVisitDirectory()` method with `if (!dir.getFileName().equals("w.e")) return FileVisitResult.SKIP_SUBTREE;`. Regardless, `walkFileTree()` will be your friend here and will produce far better code than custom for loops. – Jazzwave06 Jul 29 '16 at 18:59
  • This didnt work. Perhaps because im copying from a network drive, rather than a local drive? – ZKayyali Aug 01 '16 at 12:50
  • Debug it? I'm assuming you're on windows, and NTFS hardly make any difference between the two. Your network share implementation most likely implement file walk and file copy. Therefore, debug it. – Jazzwave06 Aug 01 '16 at 12:54
  • I figured out my error.. It was because I had new File(insertDir + file.getParent().toString() when i needed new File(insertDir + file.getParentFile().getName() – ZKayyali Aug 01 '16 at 17:13