-2

I am trying to move all files I have stored in a sub-directories to the Parent Directory they all belong to.

I am aware that this can be done through a shell script which could possibly be run through Java but was hoping for a method which could be done using Java by itself.

I'm initially using code from here: https://stackoverflow.com/a/26214647/5547474 to copy all files but it doesn't do everything I require.

Any help would be much appreciated, thanks!

Community
  • 1
  • 1
Browniez
  • 41
  • 2
  • 9

1 Answers1

4
   private static void move(File toDir, File currDir) {
        for (File file : currDir.listFiles()) {
            if (file.isDirectory()) {
                move(toDir, file);
            } else {
                file.renameTo(new File(toDir, file.getName()));
            }
        }
    }

Usage: pass it parent directory (ex. move(parentDir, parentDir)).

sssemil
  • 279
  • 6
  • 15
  • Cheers, all I needed was that line: `file.renameTo(new File(toDir, file.getName()));`. Thanks for the time and help. – Browniez Jan 12 '17 at 02:16