?
Solved

Deleting a file

Posted on 2000-01-25
5
Medium Priority
?
166 Views
Last Modified: 2013-12-25
Hi there,

Can someone help me out ? I wan't to have a script which deletes
files in a certain directory on the server, that are older than let's
say 30 day's. Is there anybody out there who can tell me how I can do this ?

Thanks in advance,
Mike

0
Comment
Question by:oostwijk
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:mattrope
ID: 2387666
You can check the file's modification date with the "stat" command, and then determine whether you want to delete it or not.

### Get a list of files in the directory
opendir(DIR, $dir_name);
@files = readdir(DIR);
closedir DIR;

### Check each file and delete if necessary

foreach $filename (@files) {
  $mtime = (stat($filename))[9];

  if ($mtime < time - $TIME_LIMIT) {
     unlink $filename;
  }
}



Obviously this assumes that your CGI script runs with sufficient permissions to list the directory contents and delete files in the directory.  $TIME_LIMIT is how old the file must be to be deleted, in seconds.  You can convert this into days with something like $NUMDAYS * 24 * 60 * 60.

Hope this is what you were looking for.
0
 

Author Comment

by:oostwijk
ID: 2389998
Mattrope, I keep getting a [500]Error message. What's wrong with the code ?
If you can help me run the script, i'll give the points to you.

#!/usr/local/bin/perl
$PAD='dummy/dummy/dummy/';
$NUMDAYS=1;
$LIMIET=$NUMDAYS*24*60*60;
### Get a list of files in the directory
opendir(DIR, $PAD);
@files=readdir(DIR);
closedir DIR;

### Check each file and delete if necessary
foreach $filename (@files) {
 $mtime=(stat($filename))[9];

 if ($mtime<time - $LIMIET) {
    unlink $filename;
 }
}
0
 

Author Comment

by:oostwijk
ID: 2390003
Adjusted points to 155
0
 
LVL 2

Accepted Solution

by:
mattrope earned 450 total points
ID: 2390148
The error may be that your script produces no output page after deleting the files.

At the end, put something like:

print "Content-type: text/plain\n\n";
print "Old Files Deleted.\n";

See if the error continues...
0
 

Author Comment

by:oostwijk
ID: 2390723
Great Job, keep up the good work !!

Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OF…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question