Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-11-2007, 11:41 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
can java.io.File create a list of all files and folders.
Hello,

My goal is to create a app that will take the folder passed to it and spit out a txt file with a list of all the files and folders with last modify date.
Example,
C:\<TAB>12/1/2007 1:00 PM
C:\config.sys<TAB>12/1/2007 1:00 PM
C:\Windows\<TAB>12/1/2007 1:00 PM
C:\windows\test.txt<TAB>12/1/2007 1:00 PM
C:\windows\Program Files\<TAB>12/1/2007 1:00 PM
C:\windows\Program Files\test.txt<TAB>12/1/2007 1:00 PM

I have played with several options from parsing a dos output, and java.io.File list /w for loop and if isDirectory. The dos output may work if I get the time and patience to tweak it and the java.io.File is a better possibility.

I would like to use java.io.File to do this the issue that I am running is it only takes the current directory and not next one. I could rap it in a method and call on every directory would this pause untill the new folder is complete? Does that make since, LoL?

Code:
public FolderList(String str) { File b = new File(str); String [] ch = b.list(); path = str; for (int i = 0; i < ch.length; i++){ File c = new File(path + ch[i]); if (c.isDirectory()){ System.out.println(path + ch[i] + "\\"); } else System.out.println(path + ch[i]); } }
Please tell me there is a easier way....
In VB6 I did something similar and used a FileBox thingy and displayed path and filename in txt box then sent to list box (list box has a max # of items, ugh!!! Java is better anyway .)

Now that I have effectivly wasted space, can you please help a little... you don't have to do the code, I would just like guidance and maybe simplistic example.

YOU ROCK...Thanks is advance, i'll shut up now.... this is so much fun, I'm going to learn more and more... thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-12-2007, 02:52 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Here is one way to do it:
Code:
public void iterate(File folder) { ArrayList<File> folders = getFolders(folder); for (int i = 0; i < folders.size(); i++) { System.out.println(folders.get(i)); // we add the subfolders of the current folder to the main FOLDERS array list folders.addAll(getFolders(folders.get(i))); // then we remove the current folder, so that we dont read it twice folders.remove(i); } } private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); // uses FileFilter to just pull out directories, then puts in an array File[] list = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isDirectory()) return true; return false; } }); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) listAr.add(list[i]); return listAr; }

Last edited by staykovmarin : 12-12-2007 at 02:56 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-12-2007, 03:38 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
??????
OK I think I get the rest, get this and not sure what todo.
Quote:
The method accept(File) of type new FileFilter(){} must override a superclass method


and just to confirm for test.
Code:
public static void main(String[] args) { FolderList myFolderList = new FolderList(); File File = new File("C:\\"); myFolderList.iterate(File);
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-12-2007, 08:18 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
File[] list = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isDirectory()) return true; return false; } });
As you can see in those few lines i AM creating a FileFilter, in this portion specifically:
Code:
@Override public boolean accept(File file) {
Is where i override accept(File file).

And yeah, thats the right way to use it, calling iterate will loop trough all the folders, printing them to command line.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-12-2007, 08:31 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
????
I will Show you what I have.... LoL...

Code:
package folderList; import java.io.File; import java.io.FileFilter; import java.util.ArrayList; public class FolderList { public void iterate(File folder) { ArrayList<File> folders = getFolders(folder); for (int i = 0; i < folders.size(); i++) { System.out.println(folders.get(i)); // we add the subfolders of the current folder to the main FOLDERS array list folders.addAll(getFolders(folders.get(i))); // then we remove the current folder, so that we dont read it twice folders.remove(i); } } private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); // uses FileFilter to just pull out directories, then puts in an array File[] list = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isDirectory()) return true; return false; } }); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) listAr.add(list[i]); return listAr; } public static void main(String[] args) { FolderList myFolderList = new FolderList(); File File = new File("C:\\"); myFolderList.iterate(File); } }

Eclipse Error...
Quote:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method accept(File) of type new FileFilter(){} must override a superclass method

at folderList.FolderList.getFolders(FolderList.java:2 5)
at folderList.FolderList.iterate(FolderList.java:10)
at folderList.FolderList.main(FolderList.java:43)
Is it not working because something I am not doing....?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-12-2007, 08:51 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
What version of java are you running?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-12-2007, 09:42 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
???
Eclipse SDK
Version: 3.2.2
Build id: M20070212-1330
JRE1.5.0_10
????downloading new Eclipse, maybe it has updates.


Java(tm) Platform, Standard Edition 6
Version 1.6.0 (build 1.6.0_03-b05)

Last edited by MattStone : 12-12-2007 at 09:58 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-12-2007, 10:02 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Try removing the @Override
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-12-2007, 10:08 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
???
Quote:
C:\765cb90a07417be0f07777bf99b52d
C:\ATI
C:\Bootable CD Project
C:\downloads
C:\eclipse
C:\ListTemplates.xml
C:\MSOCache
C:\notes
C:\QUARANTINE
C:\Rename
C:\System Volume Information
Exception in thread "main" java.lang.NullPointerException
at folderList.FolderList.getFolders(FolderList.java:3 3)
at folderList.FolderList.iterate(FolderList.java:14)
at folderList.FolderList.main(FolderList.java:43)
by the way thank you for the help.....
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-12-2007, 10:51 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Hmm the problem right now is that you are scanning files as well, so the FileFilter is not working. I have no idea why its not, unless maybe you dont have your eclipse actually using java 5+ for a compiler: Window > Prefferences > Java > Compiler, select 5 or 6 from the drop down.

Here is a revised code that doesnt use a FileFilter: (note: i am not compiling this, because i dont have java atm, but it should work)
Code:
private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); File[] list = folder.listFiles(); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) { if (listAr.add(list[i]).isDirectory()) listAr.add(list[i]); } return listAr; }
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-12-2007, 10:57 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
it is set to 5..
6 does not work dont think i have right files in this version.

just finished dl on new one ill let you know. then i will test you new code.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-13-2007, 04:14 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
OK I guess my other version of eclipse was old....

runs.... not sure if it completes but it does not error.. LoL.

Sorry for the issues I should have been on the latest version, didn't relize how dependent it might be. Learning alot YAY!

now for the questions and insanity check......LoL.
----
Let me see if I understand what you are doing.

Order of Operation not order of code...

you define the getFolders method
create a memory space
create a memory space for FileFilter
redefining what FileFilter sees by Overriding accept to only directories? (meaning only allow folders, don't care about files)
check if directory
return true or false (accept the result or don't show it)
then store directories (if true) in array (because FileFilter only allows Folders, thats all you have)

now that you have the method getFolders
use the iterate method to execute
create a memory space using a array for getFolders method
loop array to get next item
print line to screen
get folders for current folder
then remove current


Looks like this works in a small area and in large areas memory would fill up(???). Could you combine these steps and make it just hold the next item (or manual counters in a for loop) and not store in array or does it have to use array?

Last edited by MattStone : 12-13-2007 at 04:27 PM.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-13-2007, 04:19 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
Code:
private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); File[] list = folder.listFiles(); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) { if (listAr.add(list[i]).isDirectory()) listAr.add(list[i]); } return listAr; }
listAr.add(list[i]).isDirectory()
----Cannot invoke isDirectory() on the primitive type boolean
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-13-2007, 11:03 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Sorry, i didnt get a chance to run that code, so i missed up that part:
Code:
if (listAr.add(list[i]).isDirectory())
change that to :
Code:
if (list[i].isDirectory())
Edit:
I didnt see your other post
Quote:
you define the getFolders method
create a memory space
create a memory space for FileFilter
redefining what FileFilter sees by Overriding accept to only directories? (meaning only allow folders, don't care about files)
check if directory
return true or false (accept the result or don't show it)
then store directories (if true) in array (because FileFilter only allows Folders, thats all you have)

now that you have the method getFolders
use the iterate method to execute
create a memory space using a array for getFolders method
loop array to get next item
print line to screen
get folders for current folder
then remove current
getFolders returns an Arraylist of all the subfolders of the given folder. So what we do is combine the original folders ArrayList with each directories subfolders. Then we remove the folder that was scanned, so we dont scan again

Last edited by staykovmarin : 12-13-2007 at 11:15 PM.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 12-13-2007, 11:20 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
Thank no errors now. works.
please read post #12 offer any input?

again thanks for all your help.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 12-14-2007, 08:22 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
I did as I thought and BAM!......sweetness, a noob can be good at something. LoL.
Only one Array......File[]

Now how can I get a "TAB" and "last modify date" in the results? That possible? (When I copy list to excel I want column 1 folder column 2 modify date) Next step will be to get all files and folders...LoL. oooo lots of possibilities once I can do that. LoL.

Changed: to allow folders and files... that was easy once I thought about it.. took a break and then got back to it... LoL.
Changed: to allow Tab and "Last modify date" OK OK think I got what I need.... see anything that may cause issues?



Error found???? i guess the one array is filling up????? maybe write to txt and delete could fix???? any suggestions??????

Code:
package folderList; import java.io.File; import java.util.Date; public class FolderList { public void ListIt(File folder) { File[] list1 = folder.listFiles(); for (int i = 0; i < list1.length; i++) { long modTime = list1[i].lastModified(); Date d = new Date(modTime); if (list1[i].isDirectory()) { FolderList list2 = new FolderList(); list2.ListIt(list1[i]); System.out.println(list1[i] + "\t" + d.toString()); } else System.out.println(list1[i].toString() + "\t" + d.toString()); } } public static void main(String[] args) { FolderList myFolderList = new FolderList(); File File = new File("C:\\Documents and Settings\\<UserName>\\Desktop\\Copy of #Test#Test"); //File File = new File("C:\\"); myFolderList.ListIt(File); } }

Last edited by MattStone : 12-14-2007 at 09:50 PM.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 12-14-2007, 10:19 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
System.out.println(list1[i] + "\t" + d.toString());
I am not sure why you are even doing that. i would do something like this:
Code:
System.out.println(list1[i] + "\t" + list1[i].lastModified());
What error are you getting?
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 12-14-2007, 10:24 PM
Member
 
Join Date: Dec 2007
Posts: 13
MattStone is on a distinguished road
System.out.println(list1[i].lastModified());
prints a mili second string, didnt see how to format it.
1083077609416

Date d = new Date(modTime);
System.out.println(d.toString());
Fri Aug 10 14:41:29 EDT 2007

EDIT: seems to work now... do you see any issues that I might not?
EDIT: Changed package and file name.
EDIT: Seems to run into a issue.
Optional, I can figure error handeling out later. How can I capture this error and display something useful... like line the problem is on?
I just need to know why right now, is it something I am forgetting?

Exception in thread "main" java.lang.NullPointerException
at listIt.ListIt.getList(ListIt.java:18)
at listIt.ListIt.<init>(ListIt.java:12)
at listIt.ListIt.getList(ListIt.java:24)
at listIt.ListIt.<init>(ListIt.java:12)
at listIt.ListIt.main(ListIt.java:35)

Sorry I keep bugging you, you are helping me alot here. I keep playing as I can which is why you see the EDITs to keep current steps. I change the code to be the latest and error to follow just to make sure you understand. I am decent at understanding code I just lack overall knowlege and expierence I guess. Once I am done with this thing I will buy a book and try to learn more on my own. In the mean time thanks for the help. I have stayed true to my original request and just trying to get the final result. If you see me break any coding rules please let me know so I learn that part as well, I want others to be able to read it. You Rock!


Code:
package listIt; import java.io.File; import java.util.Date; public class ListIt { public ListIt(String folder) { File File = new File(folder); this.getList(File); } private void getList(File folder) { File[] list1 = folder.listFiles(); for (int i = 0; i < list1.length; i++) { long modTime = list1[i].lastModified(); Date d = new Date(modTime); if (list1[i].isDirectory()) { ListIt list2 = new ListIt(list1[i].toString()); System.out.println(list1[i] + "\\\t" + d.toString()); } else System.out.println(list1[i].toString() + "\t" + d.toString()); } } public static void main(String[] args) { //ListIt myListIt = new ListIt("C:\\Documents and Settings\\"); ListIt myListIt = new ListIt("C:\\"); } }

Last edited by MattStone : 12-15-2007 at 12:15 AM.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 12-15-2007, 02:13 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
import java.io.File; import java.util.ArrayList; import java.util.Date; public class ListIt { public ListIt(String folder) { File File = new File(folder); this.getList(File); } private void getList(File folder) { ArrayList<File> folderList = new ArrayList<File>(); // first we add all the contents of the folder that we want to scan to // the folderList folderList.addAll(getSubs(folder)); // then we iterrate trough the folder list for (int i = 0; i < folderList.size(); i++) { if (folderList.get(i).isDirectory()) { // if its a directory, we get all its subfolders/files and add them to the folderList folderList.addAll(getSubs(folderList.get(i))); } // then we print out the data: System.out.println(folderList.get(i) + "\tDate Modified:\t" + new Date(folderList.get(i).lastModified()).toString()); } } // gets the sub files of a folder public ArrayList<File> getSubs(File dest) { File[] subfiles = dest.listFiles(); ArrayList<File> subFolders = new ArrayList<File>(); // if subfiles is null, it means that listFiles() didnt return anything, // so we are dealing with a file, not a folder if (subfiles != null) // all teh contents of the folder are added to the array list for (int i = 0; i < subfiles.length; i++) { subFolders.add(subfiles[i]); } else // otherwise we jist add the file to the list subFolders.add(dest); return subFolders; } public static void main(String[] args) { // ListIt myListIt = new ListIt("C:\\Documents and Settings\\"); ListIt myListIt = new ListIt("C:\\"); } }
That should work okay for you
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 12-15-2007, 03:44 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Might want to change the for loop a little:
Code:
for (int i = 0; i < folderList.size(); i++) { if (folderList.get(i).isDirectory()) { // if its a directory, we get all its subfolders/files and add them to the folderList folderList.addAll(getSubs(folderList.get(i))); } // then we print out the data: System.out.println(folderList.get(i) + "\tDate Modified:\t" + new Date(folderList.get(i).lastModified()).toString()); // remove the current folder from the list, since we dont need it anymore folderList.remove(i); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes
Linear Mode Linear Mode