Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Howto self-delete files periodically with Perl

Reply
Thread Tools

Howto self-delete files periodically with Perl

 
 
gundalav
Guest
Posts: n/a
 
      06-05-2006
Dear expert,

Suppose I have a file created in this way:

~/MyDir/ $ perl mycode.pl > subdir/the_result.out

What I desired to do is to have a
script that : "automatically delete the files after 10 days they are
created".

What's the effective strategy to do this in Perl?
Or perhaps is there a better non-Perl solution?

Regards,
Gundala

 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      06-05-2006
gundalav wrote:
> What's the effective strategy to do this in Perl?


Look for File::Find and stat.

> Or perhaps is there a better non-Perl solution?


There is no need to do so.
However, on Unix-like systems you will manage this with find and rm too.


Marcel
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-05-2006
gundalav <(E-Mail Removed)> wrote:

> What I desired to do is to have a
> script that : "automatically delete the files after 10 days they are
> created".



That is not possible on many filesystems.

Unixy filesystems for instance, do not keep track of when a
file is created.

So, I will assume you meant "10 days they are last accessed" instead.


> What's the effective strategy to do this in Perl?



perl -le 'for (@ARGV) {print $_ if -A > 10.0}' *


Change 'print' to 'unlink' once you are convinced that it
is working OK.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-05-2006
"gundalav" <(E-Mail Removed)> wrote:
> Dear expert,
>
> Suppose I have a file created in this way:
>
> ~/MyDir/ $ perl mycode.pl > subdir/the_result.out


Presumably you have lots of files created this way, otherwise this seem
pretty trivial. OTOH, what you show is only capable of making one file,
as it just uses the same name over and over. So I don't think you've
described your problem well or completely.

>
> What I desired to do is to have a
> script that : "automatically delete the files after 10 days they are
> created".


Is "created" important, as opposed to modified or accessed?

The obvious answer to your question doesn't depend on *how* the file was
created, which (combined with your subject line) makes me wonder of you
meant to ask something different than what you did ask.

Is the program supposed to remember what files it created, so that it knows
which ones are elligible for deletion after 10 days?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      06-05-2006
gundalav wrote:
> script that : "automatically delete the files after 10 days they are created".


Your operating system doesn't know when a file was created, so Perl
doesn't know either (unless you maintain an activity log or database or
something that Perl can go to to find this information - or you could
embed the creation date as part of the filename).

The "ctime" key on the inode (available via stat) is NOT creation time
("c" stands for "change", not "create", and the value of this key
refers to the last time the file's inode was changed and will usually
match the mtime value).

--
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
gundalav
Guest
Posts: n/a
 
      06-06-2006
Thanks a lot for the reply Tad.
Few things....

>
> perl -le 'for (@ARGV) {print $_ if -A > 10.0}' *
>


What does -A stands for? Where can I find
information about this? Is there a way also to print out
the actual date (just like when we do unix's "ls -l" ) under Perl
one-liner and coupled with -A option ?

Hope to hear from you again.

Regards,
Gundala

 
Reply With Quote
 
Scott Bryce
Guest
Posts: n/a
 
      06-06-2006
gundalav wrote:

> What does -A stands for? Where can I find information about this?


http://perldoc.perl.org/functions/-X.html
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-06-2006
gundalav <(E-Mail Removed)> wrote:

> Thanks a lot for the reply Tad.



Please do not send stealth Cc's, as that is bad netiquette.


> Few things....
>
>>
>> perl -le 'for (@ARGV) {print $_ if -A > 10.0}' *
>>

>
> What does -A stands for? Where can I find
> information about this?



perldoc -f -A


> Is there a way also to print out
> the actual date



perldoc -f stat # to get the file's timestamp

perldoc -f localtime # to make it into a human-readable date


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-06-2006

Quoth (E-Mail Removed):
> gundalav <(E-Mail Removed)> wrote:
>
> > Is there a way also to print out
> > the actual date

>
> perldoc -f stat # to get the file's timestamp


You may also want to look aht the File::stat module, which provides a
much nicer interface to the same information.

Ben

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
(E-Mail Removed) (Kate Rusby)
 
Reply With Quote
 
cmic
Guest
Posts: n/a
 
      06-06-2006
Hi Gundalav

gundalav a écrit :

> Dear expert,
>
> Suppose I have a file created in this way:
>
> ~/MyDir/ $ perl mycode.pl > subdir/the_result.out
>
> What I desired to do is to have a
> script that : "automatically delete the files after 10 days they are
> created".


Why don't you use the Unix idiom (instead of Perl) :

prompt> find /mydir -ctime +5 -exec rm {} \;

Note that ctime is the modifaication time of file status, which is not
always the same as the creation time. (already noted by Filmer).
Sorry for this Out of topic...
--
cmic
>
> What's the effective strategy to do this in Perl?
> Or perhaps is there a better non-Perl solution?
>
> Regards,
> Gundala


 
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
Thunderbird - check for email periodically Jive Dadson Firefox 6 10-07-2011 11:12 AM
Periodically losing TCP connection through wireless adapter dan@sumption.org Wireless Networking 1 07-05-2006 11:12 PM
Can Tbird save a mail composition - periodically? default Firefox 5 10-16-2005 02:50 PM
Periodically loses connection. =?Utf-8?B?RnJ1c3RyYXRlZCBpbiBDbGV2ZWxhbmQ=?= Wireless Networking 5 03-28-2005 10:19 PM
Invoke function periodically via a timer H @ Q Perl 0 05-31-2004 02:49 AM



Advertisments