delete non-empty folder?

Discussion in 'Java' started by Kaidi, Oct 21, 2004.

  1. Kaidi

    Kaidi Guest

    I just wonder is there any "one step" method to delete a folder even
    it is not empty?
    I know the java.io.File has delete for empty folders. Surely we can
    write something recursively to first delete the folder's content
    before we delete the folder, but any other way?

    Thanks.
     
    Kaidi, Oct 21, 2004
    #1
    1. Advertising

  2. Kaidi

    Paul Lutus Guest

    Kaidi wrote:

    > I just wonder is there any "one step" method to delete a folder even
    > it is not empty?
    > I know the java.io.File has delete for empty folders. Surely we can
    > write something recursively to first delete the folder's content
    > before we delete the folder, but any other way?


    No, AFAIK, there is no other way within Java.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 21, 2004
    #2
    1. Advertising

  3. Kaidi

    xarax Guest

    "Paul Lutus" <> wrote in message
    news:...
    > Kaidi wrote:
    >
    > > I just wonder is there any "one step" method to delete a folder even
    > > it is not empty?
    > > I know the java.io.File has delete for empty folders. Surely we can
    > > write something recursively to first delete the folder's content
    > > before we delete the folder, but any other way?

    >
    > No, AFAIK, there is no other way within Java.


    And you have to handle the situation of nested
    folders by building your own in-memory representation
    of the folder tree structure, then delete from the bottom
    to the top using a depth-first tree walk. And you have
    to take care that you handle the situation where new
    files or folders are created by an asynchronous process
    while you're scanning/deleting.

    Good luck.
     
    xarax, Oct 21, 2004
    #3
  4. Kaidi

    Frank Guest

    xarax wrote:
    > And you have to handle the situation of nested
    > folders by building your own in-memory representation
    > of the folder tree structure, then delete from the bottom
    > to the top using a depth-first tree walk.


    Why wouldn't a simple post-order traversal work?

    E.g.:

    void delTree(File dir) {
    for each file in dir {
    if (file.isDirectory()) {
    delTree(file);
    }
    file.delete();
    } // end for
    }

    -Frank
     
    Frank, Oct 21, 2004
    #4
  5. Kaidi

    Ann Guest

    "Kaidi" <> wrote in message
    news:...
    > I just wonder is there any "one step" method to delete a folder even
    > it is not empty?
    > I know the java.io.File has delete for empty folders. Surely we can
    > write something recursively to first delete the folder's content
    > before we delete the folder, but any other way?
    >
    > Thanks.


    call a method(), that is only one step
     
    Ann, Oct 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,036
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    817
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    579
    Dave Thompson
    Feb 14, 2004
  4. =?Utf-8?B?T2xpdmllciBHSUw=?=

    Directory.Delete of a non empty dir fails in ASP.NET application

    =?Utf-8?B?T2xpdmllciBHSUw=?=, Oct 26, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    441
    Walter Wang [MSFT]
    Nov 10, 2006
  5. Raman
    Replies:
    6
    Views:
    488
    Joachim Schmitz
    Oct 29, 2007
Loading...

Share This Page