aspose file tools*
The moose likes Java in General and the fly likes Deleting files in a directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Deleting files in a directory" Watch "Deleting files in a directory" New topic
Author

Deleting files in a directory

Juanjo Alvarez
Greenhorn

Joined: Aug 31, 2007
Posts: 4
First question here, so hi all.

I have versions of compiled classes ina directory in this way:



I'd like to make a class to purge old versions. I've worked with files and RegExp, but I'm not sure how to start this taks. In some way I have to make an ordered list of this and then locate the old ones, bout I'm lost.

Any help? Thanks in advance

Juanjo
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

On way to approach this is to figure out the latest file from the time stamp. All the other ones are the oldest.
Of course the compiler can possibly create multiple class files for the same source file. So you will have to figure somehow keep a track of it and work around that problem.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Juanjo Alvarez
Greenhorn

Joined: Aug 31, 2007
Posts: 4
I have no inner classes, so there is no problem width multiple clases.

I can check the timestamp as you said or the version number, but muy main problem is how to "tokenizer" the hole list in class versions chunks, e.g.

MyClass001.class
MyClass002.class
MyOtherClass025.class
MyOtherClass026.class

I have to figure out how to get in one hand only

MyClass001.class
MyClass002.class

and next "token"

MyOtherClass025.class
MyOtherClass026.class

and so on,

and then check for deletion.

But the method java.io.File.listFiles() does not provide alphabetical order...

I don't know, maybe java is not the best option for this task; waht do you think?

Thanks in advance,
Juanjo
Juanjo Alvarez
Greenhorn

Joined: Aug 31, 2007
Posts: 4
Well, here is my solution for my particular needs. I went for number version, but timestamp also would be good:

the format of the files is ClassName_[versionNumber].class



It works. better ideas?

Thanks,
Juanjo
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Just a shot in the dark, but depending on where your classes are coming from, you might want to consider using a version control tool like subversion.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I thought sure I posted a response to this but I don't see it.

At a quick read, your algorithm keeps the first name it comes across and deletes the rest. Is that keeping the oldest and deleting the newest or did I read it wrong?

If I got it right, the simple fix is to sort backwards. Change your comparator from name1.compareTo(name2) to name2.compareTo(name1). And I think your comparator can just compare the whole name without parsing off the version number, unless you're worried about comparing name_10 to name_2 which would come out wrong. But if the version is always three digits you can just compare full names.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Juanjo Alvarez
Greenhorn

Joined: Aug 31, 2007
Posts: 4
Yes, I need the custom comparator because of the number of the digits referring the version. Can be foo_9.class and next foo_10.class

I didn't see that at the fist look (I thought it was always two), but is not fixed. It's only sure that the number is between "_" and ".class"

The algorithm keeps the first name because I need 1-n loop, but also can be 0 (which is not probable), so I need 1 + 0-n. But I'm keeping the last. note



where anterior is the first in the first loop (-: and in the next loops



anterior -> previous
actual -> current

so I delete all but the las one of the matching name-without-number. May be with time stamp would be easier.

And referring to use svn, I don't have the knowledge, I'm afraid. Also its strange, because this automatic classes generated by a middleware software, are transfered to the test/production system ("standard" procedure), so I'm not really sure how a version control system can fit there... I'll have a look anyway, thanks about that.

Cheers,

Juanjo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deleting files in a directory
 
Similar Threads
Hot Deployment in Wenblogic 6.1
Do I need my own ClassLoader? (And why?)
Disambiguating a class defined in multiple libs
Cleaning out old versions in git
How Java handles class files?