Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reading Sub Directories (Random) and deleting its contents?

Reply
Thread Tools

Reading Sub Directories (Random) and deleting its contents?

 
 
gaur.ritesh@gmail.com
Guest
Posts: n/a
 
      03-02-2008
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

 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      03-02-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
RG
Guest
Posts: n/a
 
      03-02-2008
On Mar 3, 12:37 am, Arne Vajhøj <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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[i].isDirectory ( ) )
System.out.println ( "Directory: " + strFilesDirs[i] ) ;

else if ( strFilesDirs[i].isFile ( ) )
System.out.println ( "File: " + strFilesDirs[i] + " (" +
strFilesDirs[i].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)
 
Reply With Quote
 
Christian
Guest
Posts: n/a
 
      03-02-2008
How about writting a method that recursivele deletes everything ..

maybe recursion would do the job?

Christian
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-02-2008
On 02.03.2008 18:15, RG wrote:
> On Mar 3, 12:37 am, Arne Vajhøj <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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[i].isDirectory ( ) )
> System.out.println ( "Directory: " + strFilesDirs[i] ) ;
>
> else if ( strFilesDirs[i].isFile ( ) )
> System.out.println ( "File: " + strFilesDirs[i] + " (" +
> strFilesDirs[i].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

 
Reply With Quote
 
petersprc
Guest
Posts: n/a
 
      03-02-2008
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 <(E-Mail Removed)> wrote:
> On 02.03.2008 18:15, RG wrote:
>
>
>
> > On Mar 3, 12:37 am, Arne Vajhøj <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) 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[i].isDirectory ( ) )
> > System.out.println ( "Directory: " + strFilesDirs[i] ) ;

>
> > else if ( strFilesDirs[i].isFile ( ) )
> > System.out.println ( "File: " + strFilesDirs[i] + " (" +
> > strFilesDirs[i].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


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-03-2008
On Sun, 2 Mar 2008 07:44:59 -0800 (PST), (E-Mail Removed) 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-03-2008
On Sun, 2 Mar 2008 09:15:21 -0800 (PST), RG <(E-Mail Removed)>
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
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      03-17-2008
RG wrote:
> On Mar 3, 12:37 am, Arne Vajhøj <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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[i].isDirectory ( ) )
> System.out.println ( "Directory: " + strFilesDirs[i] ) ;
>
> else if ( strFilesDirs[i].isFile ( ) )
> System.out.println ( "File: " + strFilesDirs[i] + " (" +
> strFilesDirs[i].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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
recursive output of a directory and its sub-directories Alexander Fleck Ruby 2 07-06-2007 08:52 AM
How to compress a folder and all of its sub directories and filesinto a zip file? could ildg Python 0 06-28-2005 06:16 AM
Extracting Directories and Sub Directories and Counting Ron Smith Perl Misc 5 11-02-2004 11:23 PM
How to map Project directories to Production sub-directories Joel Finkel ASP .Net 0 09-12-2003 06:47 PM



Advertisments