Reading Sub Directories (Random) and deleting its contents?

G

gaur.ritesh

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 :)
 
A

Arne Vajhøj

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
 
R

RG

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)
 
C

Christian

How about writting a method that recursivele deletes everything ..

maybe recursion would do the job?

Christian
 
R

Robert Klemme

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
 
P

petersprc

Hi,

Here's the code:

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

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


YA! Thats True, but I also need to delete the contents inside them.
Sample Code .......
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:

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
 
R

Roedy Green

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
 
A

Arne Vajhøj

RG said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top