Is it possible to copy directory names without copying their contents?

4

For some time I've done this question about recursive search in folders, and now I need to adapt to a different condition.

I need to copy just the names of subfolders from a top folder to a third folder, but without copying the most internal files and folders, that is, I need to "clone" the top-level subfolders without bringing their content together.

ex.:

-Pasta root
    |-subpasta1✅
        |-sub-subpasta1(esse nivel não pode ser copiado)
    |-subpasta2 ✅
    |-subpasta3✅
    |-subpasta4✅
    ...
    |-subpastaN✅

I'm currently using a suggested method in the linked question to filter files:

private static List<File> filtrarArquivos(File source, String pattern) throws IOException {
    List<File> fileList = new ArrayList<>();

    Files.walk(source.toPath()).forEach(arquivo -> {

        if (arquivo.getFileName().toString().matches(pattern)) {
            fileList.add(arquivo.toFile());
        }
    });

    return fileList;
}

Is it possible to adapt this method to the reported problem?

    
asked by anonymous 20.02.2017 / 17:04

2 answers

5

Changing your method you can do as follows:

private static List<File> filtrarDiretorios(File origem) throws IOException {
  List<File> diretorios = new ArrayList<>();

  Files.list(origem.toPath())
          .map(arquivo -> arquivo.toFile())
          .filter(arquivo -> arquivo.isDirectory())
          .forEach(diretorios::add);

  return diretorios;
}

We use Steram#map to convert the contents of the list returned by Path#list " in a Stream of File . We used the method Stream#filter of Stream resulting to filter only File that are a directory with the File#isDirectory and walk through the resulting records with Stream#forEach " to add them to the List that will return the filtrarArquivos method.

You can apply the method above as follows (Completing the copy of the folders):

private static void copiarDiretorios(File origem, File destino) throws IOException {
  List<File> diretorios;

  diretorios = filtrarDiretorios(origem);
  diretorios.forEach(diretorio -> copiar(destino, diretorio));
}

private static void copiar(File destino, File diretorio) {
  File novo;

  novo = new File(destino, diretorio.getName());
  novo.mkdirs();
}
    
20.02.2017 / 17:57
4

The following method copies all the direct children file from a source folder to a destination folder:

public static final List<File> copiarSubdiretorios(File origem, File destino) throws IOException {
    List<File> arquivos = new ArrayList<>();
    for(File arquivo : origem.listFiles(File::isDirectory)) {
        File novoArquivo = new File(destino.getAbsolutePath() + "/" + arquivo.getName());
        novoArquivo.mkdirs();
        arquivos.add(novoArquivo);
    }
    return arquivos;
}

Edit:

If you just want the name of the subfolders do the following:

public static final List<String> listarSubdiretorios(File root) {
    return Arrays.asList(root.listFiles(File::isDirectory))
                .stream()
                .map(File::getName)
                .collect(Collectors.toList());
}

If you want% of% objects representing the folders:

public static final List<File> listarSubdiretorios(File root) {
    return Arrays.asList(root.listFiles(File::isDirectory));
}
    
20.02.2017 / 18:14