Results 1 to 4 of 4
  1. #1
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default [SOLVED] To Delete All subdir in a directory

    import java.io.File;

    class DeleteDir {
    public static void main(String args[]) {
    deleteDirectory(new File(args[0]));
    }

    static public boolean deleteDirectory(File path) {
    if( path.exists() ) {
    File[] files = path.listFiles();
    for(int i=0; i<files.length; i++) {
    if(files[i].isDirectory()) {
    deleteDirectory(files[i]);
    }
    else {
    files[i].delete();
    }
    }
    }
    return( path.delete() );
    }
    }

    Could anyone tell me that where I should write the dir name which I want to del?
    A stitch in time saves nine:D

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    10

    Question no need to keep name

    Quote Originally Posted by tOpach View Post
    Could anyone tell me that where I should write the dir name which I want to del?
    Well, you could do this:
    Java Code:
    class DeleteDir {
        // name of ?.....
        String fileToDelete;
        DeleteDir(String fileToDelete){
            fileToDelete = fileToDelete;
        }
      public static void main(String args[]) {
          File aFile = new File(args[0]);
          DeleteDir dd = new DeleteDir(args[0]);
          deleteDirectory(aFile));
      }
    }
    but why do we need the file name?

    Looks very similar to recursive directory search, or is something similar.

    I am working on recursive directory search this morning, if file is to be deleted - why do we need the name?

    (errors in code while getting clarifications)
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    Dear Nicholas,

    Thank you for quick reply. I try to make a telephone directory. I wrote some programme code which allows user to add new people. When you add a new person, program creates a folder and some txt files into that folder. Now I just want my program to delete a person's folder and all its sub directories which will be entered by user. Thats what I am trying to do.

    Thanks for help:)


    Kind Regards,

    Salih Ozan IRBIK
    A stitch in time saves nine:D

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    10

    Default

    Try Web4J
    I have looked around and this coder knows good practices and so on. I copied a recursive directory search, but noticed it had a copyright attached so suggest just dig around Hirondelle's site - some work involved but is just what you need.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. How to delete the records
    By kiran kumar in forum Java Servlet
    Replies: 6
    Last Post: 11-09-2008, 02:16 PM
  2. Delete
    By Sarinam in forum New To Java
    Replies: 6
    Last Post: 07-23-2008, 11:09 AM
  3. Listbox Add/Delete
    By Rageagainst20 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 04:49 PM
  4. Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 AM

Posting Permissions

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