Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Question 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?

    Java 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!!! :D Java is better anyway :D .)

    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.:) :) :) :)

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Here is one way to do it:
    Java 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 01:56 AM.

  3. #3
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Question ??????

    OK I think I get the rest, get this and not sure what todo.
    The method accept(File) of type new FileFilter(){} must override a superclass method
    :confused:

    and just to confirm for test.
    Java Code:
    	public static void main(String[] args) {
    		FolderList myFolderList = new FolderList(); 
    		File File = new File("C:\\");
    		myFolderList.iterate(File);
    :D

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java 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:
    Java 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.

  5. #5
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Question ????

    :confused: I will Show you what I have.... LoL...

    Java 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...
    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....?
    :confused:

  6. #6
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    What version of java are you running?

  7. #7
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default ???

    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 08:58 PM.

  8. #8
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Try removing the @Override

  9. #9
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Question ???

    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.....

  10. #10
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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)
    Java 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;
           }

  11. #11
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    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.

  12. #12
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    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 03:27 PM.

  13. #13
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    Java 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

  14. #14
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Sorry, i didnt get a chance to run that code, so i missed up that part:
    Java Code:
    if (listAr.add(list[i]).isDirectory())
    change that to :
    Java Code:
    if (list[i].isDirectory())
    Edit:
    I didnt see your other post
    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 10:15 PM.

  15. #15
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    Thank no errors now. works.
    please read post #12 offer any input?

    again thanks for all your help.

  16. #16
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Question

    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??????

    Java 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 08:50 PM.

  17. #17
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java 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:
    Java Code:
    System.out.println(list1[i] + "\t" + list1[i].lastModified());
    What error are you getting?

  18. #18
    MattStone is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    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!


    Java 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-14-2007 at 11:15 PM.

  19. #19
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java 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

  20. #20
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Might want to change the for loop a little:
    Java 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);
    			
    		}

Page 1 of 2 12 LastLast

Similar Threads

  1. How to create a Sorted List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:31 PM
  2. JAR file with .java files
    By ravian in forum New To Java
    Replies: 7
    Last Post: 12-24-2007, 08:15 PM
  3. Replies: 2
    Last Post: 11-27-2007, 01:45 PM
  4. URGENTTT...zip folders in java
    By rach in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 03:55 PM
  5. Create view of files in java-Swing
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2007, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •