Reading Sub Directories (Random) and deleting its contents?

Discussion in 'Java' started by gaur.ritesh@gmail.com, Mar 2, 2008.

  1. Guest

    Hi,

    I am looking for a code to read sub directories that are created
    randomly by "another program".i.e the name of the directories are not
    known in advance.

    For Example:

    Root Dir is "Systems01"
    Under "Systems01" they are many Directories (created by a program
    randomly)

    For example"

    Systems01
    ------alpha
    ------qwerty
    ------ggrt
    ------remake

    Next time I will start my computer these sub directories will be
    different (alpha,qwerty....), so I need to make a generalize program
    to delete these directories as well as contents.


    Please note: "Another program" is not a virus, its a java program :)
    , Mar 2, 2008
    #1
    1. Advertising

  2. Arne Vajhøj Guest

    wrote:
    > I am looking for a code to read sub directories that are created
    > randomly by "another program".i.e the name of the directories are not
    > known in advance.


    The File class has methods to find all subdirectories in a given directory.

    Arne
    Arne Vajhøj, Mar 2, 2008
    #2
    1. Advertising

  3. RG Guest

    On Mar 3, 12:37 am, Arne Vajhøj <> wrote:
    > wrote:
    > > I am looking for a code to read sub directories that are created
    > > randomly by "another program".i.e the name of the directories are not
    > > known in advance.

    >
    > The File class has methods to find all subdirectories in a given directory..
    >
    > Arne


    YA! Thats True, but I also need to delete the contents inside them.

    Sample Code .......

    ------------------------
    File f1 = new File ( "C:\\Systems01") ;


    File[] strFilesDirs = f1.listFiles ( );

    for ( int i = 0 ; i < strFilesDirs.length ; i ++ ) {
    if ( strFilesDirs.isDirectory ( ) )
    System.out.println ( "Directory: " + strFilesDirs ) ;

    else if ( strFilesDirs.isFile ( ) )
    System.out.println ( "File: " + strFilesDirs + " (" +
    strFilesDirs.length ( ) + ")" ) ;

    -------------------------------------------------
    This is what I am using, the problem is to delete the particular
    content from a sub directory.
    Such As:

    Systems01
    ---Alpha
    -----Kilo
    -------Temp

    I need to delete the contents of the Temp Directory!

    Alpha is a random name, but the inside structure is going to be same
    (Kilo>Temp)
    RG, Mar 2, 2008
    #3
  4. Christian Guest

    How about writting a method that recursivele deletes everything ..

    maybe recursion would do the job?

    Christian
    Christian, Mar 2, 2008
    #4
  5. On 02.03.2008 18:15, RG wrote:
    > On Mar 3, 12:37 am, Arne Vajhøj <> wrote:
    >> wrote:
    >>> I am looking for a code to read sub directories that are created
    >>> randomly by "another program".i.e the name of the directories are not
    >>> known in advance.

    >> The File class has methods to find all subdirectories in a given directory.
    >>
    >> Arne

    >
    > YA! Thats True, but I also need to delete the contents inside them.
    >
    > Sample Code .......
    >
    > ------------------------
    > File f1 = new File ( "C:\\Systems01") ;
    >
    >
    > File[] strFilesDirs = f1.listFiles ( );
    >
    > for ( int i = 0 ; i < strFilesDirs.length ; i ++ ) {
    > if ( strFilesDirs.isDirectory ( ) )
    > System.out.println ( "Directory: " + strFilesDirs ) ;
    >
    > else if ( strFilesDirs.isFile ( ) )
    > System.out.println ( "File: " + strFilesDirs + " (" +
    > strFilesDirs.length ( ) + ")" ) ;
    >
    > -------------------------------------------------
    > This is what I am using, the problem is to delete the particular
    > content from a sub directory.
    > Such As:
    >
    > Systems01
    > ---Alpha
    > -----Kilo
    > -------Temp
    >
    > I need to delete the contents of the Temp Directory!
    >
    > Alpha is a random name, but the inside structure is going to be same
    > (Kilo>Temp)


    Does it need to be a Java program? I'm asking because usually this can
    be easily done by standard tools available on your operating system
    ("del" on windows or "rm -rf", "find ... | xargs rm" on Unixes).

    Kind regards

    robert
    Robert Klemme, Mar 2, 2008
    #5
  6. petersprc Guest

    Hi,

    Here's the code:

    http://www.rgagnon.com/javadetails/java-0483.html

    Probably better than using Runtime.exec which would be less
    portable...


    On Mar 2, 12:45 pm, Robert Klemme <> wrote:
    > On 02.03.2008 18:15, RG wrote:
    >
    >
    >
    > > On Mar 3, 12:37 am, Arne Vajhøj <> wrote:
    > >> wrote:
    > >>> I am looking for a code to read sub directories that are created
    > >>> randomly by "another program".i.e the name of the directories are not
    > >>> known in advance.
    > >> The File class has methods to find all subdirectories in a given directory.

    >
    > >> Arne

    >
    > > YA! Thats True, but I also need to delete the contents inside them.

    >
    > > Sample Code .......

    >
    > > ------------------------
    > > File f1 = new File ( "C:\\Systems01") ;

    >
    > > File[] strFilesDirs = f1.listFiles ( );

    >
    > > for ( int i = 0 ; i < strFilesDirs.length ; i ++ ) {
    > > if ( strFilesDirs.isDirectory ( ) )
    > > System.out.println ( "Directory: " + strFilesDirs ) ;

    >
    > > else if ( strFilesDirs.isFile ( ) )
    > > System.out.println ( "File: " + strFilesDirs + " (" +
    > > strFilesDirs.length ( ) + ")" ) ;

    >
    > > -------------------------------------------------
    > > This is what I am using, the problem is to delete the particular
    > > content from a sub directory.
    > > Such As:

    >
    > > Systems01
    > > ---Alpha
    > > -----Kilo
    > > -------Temp

    >
    > > I need to delete the contents of the Temp Directory!

    >
    > > Alpha is a random name, but the inside structure is going to be same
    > > (Kilo>Temp)

    >
    > Does it need to be a Java program? I'm asking because usually this can
    > be easily done by standard tools available on your operating system
    > ("del" on windows or "rm -rf", "find ... | xargs rm" on Unixes).
    >
    > Kind regards
    >
    > robert
    petersprc, Mar 2, 2008
    #6
  7. Roedy Green Guest

    On Sun, 2 Mar 2008 07:44:59 -0800 (PST), wrote,
    quoted or indirectly quoted someone who said :

    >
    >Next time I will start my computer these sub directories will be
    >different (alpha,qwerty....), so I need to make a generalize program
    >to delete these directories as well as contents.


    for code to do it, see http://mindprod.com/products1.html#BATIK
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Mar 3, 2008
    #7
  8. Roedy Green Guest

    On Sun, 2 Mar 2008 09:15:21 -0800 (PST), RG <>
    wrote, quoted or indirectly quoted someone who said :

    >YA! Thats True, but I also need to delete the contents inside them.


    you mean wipe the file for security? When you delete the file the
    sectors it occupies are put on the free space chain to be reused.
    normally that is sufficient.

    The CIA can recover data even if you wipe several times. See
    http://mindprod.com/jgloss/wipe.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Mar 3, 2008
    #8
  9. Arne Vajhøj Guest

    RG wrote:
    > On Mar 3, 12:37 am, Arne Vajhøj <> wrote:
    >> wrote:
    >>> I am looking for a code to read sub directories that are created
    >>> randomly by "another program".i.e the name of the directories are not
    >>> known in advance.

    >> The File class has methods to find all subdirectories in a given directory.

    >
    > YA! Thats True, but I also need to delete the contents inside them.
    >
    > Sample Code .......
    >
    > ------------------------
    > File f1 = new File ( "C:\\Systems01") ;
    >
    >
    > File[] strFilesDirs = f1.listFiles ( );
    >
    > for ( int i = 0 ; i < strFilesDirs.length ; i ++ ) {
    > if ( strFilesDirs.isDirectory ( ) )
    > System.out.println ( "Directory: " + strFilesDirs ) ;
    >
    > else if ( strFilesDirs.isFile ( ) )
    > System.out.println ( "File: " + strFilesDirs + " (" +
    > strFilesDirs.length ( ) + ")" ) ;
    >
    > -------------------------------------------------
    > This is what I am using, the problem is to delete the particular
    > content from a sub directory.
    > Such As:
    >
    > Systems01
    > ---Alpha
    > -----Kilo
    > -------Temp
    >
    > I need to delete the contents of the Temp Directory!
    >
    > Alpha is a random name, but the inside structure is going to be same
    > (Kilo>Temp)


    You can recurse through multiple levels of directories.

    If you have an "order of delete" problem, then put the
    to be deleted stuff in a collection in the correct order
    and delete from that in the end.

    Arne
    Arne Vajhøj, Mar 17, 2008
    #9
    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. Joel Finkel
    Replies:
    0
    Views:
    490
    Joel Finkel
    Sep 12, 2003
  2. could ildg
    Replies:
    0
    Views:
    301
    could ildg
    Jun 28, 2005
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,016
    Lawrence D'Oliveiro
    May 20, 2011
  4. Alexander Fleck
    Replies:
    2
    Views:
    135
    Alexander Fleck
    Jul 6, 2007
  5. Ron Smith
    Replies:
    5
    Views:
    144
    Michele Dondi
    Nov 2, 2004
Loading...

Share This Page