0
public static void VDirectoryMaker(File des) {
        if (!des.exists()) {
            File dest = new File(des.getAbsolutePath().substring(0, des.getAbsolutePath().length()-(des.getName().length())));
            VDirectoryMaker(dest);
            dest.mkdir();
        }
    }

If des equals new File("dir1\\dir2\\dir3\\dir4\\dir5") why does it only make all non-existing folders up to dir4? dir5 is never created.

FOD
  • 333
  • 6
  • 16
  • 1
    Use [mkdirs()](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs%28%29) instead [mkdir()](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdir%28%29). – OO7 Mar 23 '15 at 18:20
  • Have a look at [Difference between mkdir() and mkdirs() in java for java.io.File](http://stackoverflow.com/questions/9820088/difference-between-mkdir-and-mkdirs-in-java-for-java-io-file). It'll give u answer. – OO7 Mar 23 '15 at 18:28

2 Answers2

1

It's because the last dir5 is treated as file without extension inside the dir4 directory.

Try adding last backslash, the directories should be created as you expected.

new File("dir1\\dir2\\dir3\\dir4\\dir5\\")

You should also look at File.mkdirs().

fracz
  • 20,536
  • 18
  • 103
  • 149
0

This code is removing the last part of the path:

des.getAbsolutePath().substring(
  0, des.getAbsolutePath().length()-(des.getName().length()))

This would work if your input was a complete file path for a file, eg:

VDirectoryMaker(new File("dir1\\dir2\\dir3\\dir4\\targetfile.txt");
Alex Fitzpatrick
  • 643
  • 5
  • 10