0

I need to build a tool to copy files from and to a dated directory. (At the same time)

Example User Input, Enter file to copy: C:\Users\asmuhamm\Documents\test.txt

Enter from date: 221012 or C:\Users\asmuhamm\Documents\221012

Enter to date: 221020 or C:\Users\asmuhamm\Documents\221020

File test.txt will be copied to folder 221012 to 221020 (also copy to folders in-between 221013, 221014, 221015 .... etc)

        System.out.println("Do you want to create dated folders?");
        String answer1 = scanner.nextLine();

            if (Objects.equals(answer1, "yes")) {
                //do something
                System.out.println("Enter folder from date: ");
                int from = scanner.nextInt();

                System.out.println("Enter folder to date: ");
                int to = scanner.nextInt();


                String myDirectoryPath = "C:\\Users\\asmuhamm\\Documents\\";


                for (int i = from; i <= to; i++){

                    File myFolder = new File(myDirectoryPath + i);
                    if (!myFolder.exists()) {

                        if (myFolder.mkdirs()){
                            System.out.println(myFolder.getName() + " created successfully.");


                        } else {
                            System.out.println("Error creating " + myFolder.getName());
                        }
                    } else {
                        System.out.println(myFolder.getName() + " already exists.");
                    }
                }

            } else {
                System.exit(0);
            }


    System.out.println("Do you want to copy files into folders? ");
    String answer2 = scanner.next();

    if (Objects.equals(answer2, "yes")) {
        String again;
        do {
            System.out.println("Enter file source: ");
            String source = scanner.next();

            System.out.println("Enter file target: ");
            String target = scanner.next();

            Path fsource = Paths.get(source);
            Path ftarget = Paths.get(target);

            Files.copy(fsource, ftarget);
            System.out.println("File copy successful");

            System.out.println("Copy again? ");
            again = scanner.next();
        } while (again.equals("yes"));
    } else{
        System.exit(0);
    }


}

}

Currently I can only copy it one by one instead of copying it from and to in a range.

Ash
  • 1
  • 2
  • All of the ingredients are already in this code. The for loop ensures each destination directory exists. It needs a similar for loop where it actually copies the file to use each of those destinations. – Tim Moore Oct 22 '22 at 05:58
  • Does this answer your question? [How to copy and move large amount of .txt files in one folder to multiple different subfolders in Java, based on certain characters in their filename?](https://stackoverflow.com/questions/25047289/how-to-copy-and-move-large-amount-of-txt-files-in-one-folder-to-multiple-differ) – Kai Burghardt Oct 22 '22 at 10:36

0 Answers0