[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Better way to kill files?

Cory

10/13/2007 5:10:00 PM

Purpose here is to find all files > 5 hours old in a directory and wipe
them...

Of course I could just write a shell script but I wanted to leverage
ruby...or, rather, needed to leverage ruby...not real familiar with the
File and FileUtils API just yet...

dir_path = "~/foo/bar"

Dir.new(dir_path).each {|file|
if file.include?('foobar')
afile = File.new("#{dir_path}/#{file}")

if (Time.now - afile.mtime)/60/60 > 5
File.delete("#{dir_path}/#{file}")
end
end
}
--
Posted via http://www.ruby-....

3 Answers

Rick DeNatale

10/13/2007 5:53:00 PM

0

On 10/13/07, Cory Wilkerson <coryw@americanmonkey.com> wrote:
> Purpose here is to find all files > 5 hours old in a directory and wipe
> them...
>
> Of course I could just write a shell script but I wanted to leverage
> ruby...or, rather, needed to leverage ruby...

Not to discourage you from doing it in ruby, but you don't need to
write a shell script:

$man find

$find ~/foo/bar -mmin 300 -delete


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Brian Adkins

10/13/2007 6:05:00 PM

0

On Oct 13, 1:53 pm, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
> On 10/13/07, Cory Wilkerson <co...@americanmonkey.com> wrote:
>
> > Purpose here is to find all files > 5 hours old in a directory and wipe
> > them...
>
> > Of course I could just write a shell script but I wanted to leverage
> > ruby...or, rather, needed to leverage ruby...
>
> Not to discourage you from doing it in ruby, but you don't need to
> write a shell script:
>
> $man find
>
> $find ~/foo/bar -mmin 300 -delete

ruby -e '`find ~/foo/bar -mmin 300 -delete`'

In case the boss says it has to be Ruby :)

John Joyce

10/13/2007 6:42:00 PM

0


On Oct 13, 2007, at 1:10 PM, Brian Adkins wrote:

> On Oct 13, 1:53 pm, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
>> On 10/13/07, Cory Wilkerson <co...@americanmonkey.com> wrote:
>>
>>> Purpose here is to find all files > 5 hours old in a directory
>>> and wipe
>>> them...
>>
>>> Of course I could just write a shell script but I wanted to leverage
>>> ruby...or, rather, needed to leverage ruby...
>>
>> Not to discourage you from doing it in ruby, but you don't need to
>> write a shell script:
>>
>> $man find
>>
>> $find ~/foo/bar -mmin 300 -delete
>
> ruby -e '`find ~/foo/bar -mmin 300 -delete`'
>
> In case the boss says it has to be Ruby :)
>
>
Of course you might want to generate a log of what got deleted.
Or perhaps just to stdout...
Then again, you might even want to do that and move them to a temp
dir for later deletion if they're not important.
Definitely want a log of anything that fit the requirements but was
unable to delete!