aspose file tools*
The moose likes Java in General and the fly likes Show dir and file structure exactly as it is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Show dir and file structure exactly as it is" Watch "Show dir and file structure exactly as it is" New topic
Author

Show dir and file structure exactly as it is

Adrian Airmil
Greenhorn

Joined: Dec 12, 2005
Posts: 24
I'm trying to show all directories and if a dir contains files i want to show all of them before moving on to the next dir.

I have a dir called imagetest. This contains a dir named 'folder1'. This contains 2 files (folder1_img1.jpg and folder1_img2.jpg) as well as 2 sub dirs folder2a and folder2b. folder2a contains 1 file and folder2b contains 2 files.

Currently testing this on my localhost but eventually it will run on unix.

consider this code


the results are:
Directory imagetest
Directory folder1
--->File folder1_img1.jpg
--->File folder1_img2.jpg
Directory folder2a
--->File folder2a_img1.jpg
Directory folder2b
--->File folder2b_img1.jpg
--->File folder2b_img2.jpg

which are correct. However if i rename 'folder1_img2.jpg' to 'folder2_img2.jpg', the results are:
Directory imagetest
Directory folder1
--->File folder1_img1.jpg
Directory folder2a
--->File folder2a_img1.jpg
Directory folder2b
--->File folder2b_img1.jpg
--->File folder2b_img2.jpg
--->File folder2_img2.jpg

This is not right as the renamed file now shows up under dir folder2b. Where as its location has not changed as it is still in dir folder1. I guess my process() needs to be cleaned up. Appreciate any help in listing the dir and files as per their nested locations.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
The problem comes from that you simply use the result of dir.list() without checking whether they are files or direcories.
Whenever the result returns:
file1
file2
dir1
file3
You will display wrong value, since you will display file3 after displaying dir1 so you list it under dir1.
You can check all the children and display the files first and only display the dirs after that. Or you can add tabulators depending on the deepness so then the structure will appear corrrectly.
Supposing dir1 contains 2 file:
file1
file2
dir1
----dir1file1
----dir1file2
file3
Adrian Airmil
Greenhorn

Joined: Dec 12, 2005
Posts: 24
Thanks for your response
Miklos Szeles wrote:
You can check all the children and display the files first and only display the dirs after that.

So I've modified the code as follows and it works. Let me know if there are any errors.

result is:
Directory imagetest
Directory folder1
--->File folder1_img1.jpg
--->File folder2_img2.jpg
Directory folder2a
--->File folder2a_img1.jpg
Directory folder2b
--->File folder2b_img1.jpg
--->File folder2b_img2.jpg


Miklos Szeles wrote:Or you can add tabulators depending on the deepness so then the structure will appear corrrectly.

I'd like to do this. Could you give me some suggestions on how to do this?
>
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Modify the method to public static void visitAllDirsAndFiles(File dir, int level) and the you can call recursively by
visitAllDirsAndFiles(files[i], level + 1);
You can print the tabs accordint to the level.
Adrian Airmil
Greenhorn

Joined: Dec 12, 2005
Posts: 24
Miklos Szeles wrote:Modify the method to public static void visitAllDirsAndFiles(File dir, int level) and the you can call recursively by
visitAllDirsAndFiles(files[i], level + 1);
You can print the tabs accordint to the level.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Show dir and file structure exactly as it is