2

I have folder structure as below and I need to check and copy files from AB1 folder to CD1 folder.

Root Folder
|___AB ------------------------>DD
    |__AB1 and AB2(folders)      |_AB1 and AB2
        |_CD1                       |__CD1 |__CD2

But it is checking for CD1 in both AB1 and AB2 folders and files are not generating.

File file = new File("workspace");
List<File> abFolders = getDir(file); //level 1 folders
abFolders = new ArrayList<File>(abFolders);
List<File> cdFolders = getSubdirs(abFolders); //level 2 folders

//print
for (File file1 : abFolders) {
    for(File file2:cdFolders){
     //Here I need to check if CD folder is present in AB1 then it has to copy to CD1 and not to check for another CD1 folder in AB2 folder.
         System.out.println(file1.getName());
         System.out.println(file2.getName());
    }
    
}

private static List<File> getDir(File file) {
    List<File> subdirs = Arrays.asList(file.listFiles(new FileFilter() {
        public boolean accept(File f) {
            return f.isDirectory();
        }
    }));
    return subdirs;
}
static List<File> getSubdirs(List<File> subdirs) {
    List<File> deepSubdirs = new ArrayList<File>();
    for(File subdir : subdirs) {
        deepSubdirs.addAll(getDir(subdir)); 
    }
    //subdirs.addAll(deepSubdirs);
    return deepSubdirs;
}
14
  • 1
    Is CD1 folder, INSIDE the AB1 folder? Commented Aug 13, 2020 at 8:13
  • @L.Papadopoulos yes CD1 Folder is inside of AB1 Folder Commented Aug 13, 2020 at 8:21
  • 1
    So you want to transfer, all neighbor folders of CD1, inside CD1? Commented Aug 13, 2020 at 8:22
  • Now it is selecting the folders like first AB2 and checking for CD1 and if it is not present code stops there....and it wasn't checking for AB1 of CD1 folder Commented Aug 13, 2020 at 8:24
  • 1
    So, it does not even check AB1? Commented Aug 13, 2020 at 8:36

1 Answer 1

3

Use collections, so that you sort abFolders and cdFolders. First import the library:

import java.util.Collections;

Then do the sort of AB folders, before entering loop.

Collections.sort(abFolders);
for (File file1 : abFolders) {
    for(File file2:cdFolders){
     //Here I need to check if CD folder is present in AB1 then it has to copy to CD1 and not to check for another CD1 folder in AB2 folder.
         System.out.println(file1.getName());
         System.out.println(file2.getName());
    }
    
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok I will check it. But why dont you ask a new question for that problem? That way, more people (and maybe more experts) will be able to answer.
i was not able to add the question it is saying like ....you were not able to ask the question for longer time'
I know, I will check it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.