Results 1 to 4 of 4
  1. #1
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default How do we "Loop" through subfolders to delete files?

    Hi Everyone, I have some example code bellow it works and was a tutorial on the web somewhere I forget where I got it but I would like to modify it. I have been searching for days on this mod but with no luck so here is my question: How do you make this work to include or "LOOP" through all the subfolders and do the delete there as well?

    Thanks in advance for any direction.

    Java Code:
    import java.io.File;
    import java.io.FilenameFilter;
    
    public class delfile {
    
    	/**
    	 * @param args
    	 */
    		   public static void main (String args[]) {
    		     System.out.println("hello");
    		     delfile td = new delfile();
    		     td.deleteFiles("c:/temp2/", ".csv");
    		   }
    
    		   public void deleteFiles( String d, String e ) {
    
    		     ExtensionFilter filter = new ExtensionFilter(e);
    		     File dir = new File(d);
    
    		     String[] list = dir.list(filter);
    		     File file;
    		     if (list.length == 0) return;
    
    		     for (int i = 0; i < list.length; i++) {
    		       file = new File(d + list[i]);
    		       boolean isdeleted =   file.delete();
    		       System.out.print(file);
    		       System.out.println( "  deleted " + isdeleted);
    		     }
    		   }
    
    		   class ExtensionFilter implements FilenameFilter {
    
    		     private String extension;
    
    		     public ExtensionFilter( String extension ) {
    		       this.extension = extension;             
    		     }
    		     public boolean accept(File dir, String name) {
    		       return (name.endsWith(extension));
    		     }
    		   }
    
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    In deleteFiles check if the file from the list is a directory. If it is, call deleteFiles with that file as directory.
    Change accept() in the filter to "accept" directories.

  3. #3
    Zrob is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Thanks for the insight on this, I will give that a try.

    Will I need to include some recursion, or can I just use a While loop?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I've always use recursion. It is easier for me to code. I don't know how hard loops would be. Try it and let us see the results.

    For testing, DON'T do any real deletes. Just print a message. If you have a program bug, you can really hurt yourself if the program wanders off.

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  3. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM
  4. ".jar" files
    By tim in forum New To Java
    Replies: 12
    Last Post: 02-07-2008, 11:23 PM
  5. Windows ".exe" files
    By tim in forum New To Java
    Replies: 3
    Last Post: 12-28-2007, 09:25 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
  •