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.