Forum Moderators: coopster & phranque

Message Too Old, No Replies

php to perl

         

lindajames

1:10 am on Aug 22, 2003 (gmt 0)

10+ Year Member



i have this code below that basically deletes the content of an entire folder called "temp", but the thing is its in PHP and i need to get perl to do it. So, if someone can help me convert it to perl i would appreciate it.

cheers
linda

<?php
$dh = opendir(temp) or die ("Could not open temp directory");
while (false!== ($file = readdir($dh)))
{
if ($file!= "." && $file!= "..")
{
unlink("temp/$file") ;
echo "<font face=Verdana size=1>All Files Deleted...</font>";
}
}
?>

myself

7:44 am on Aug 22, 2003 (gmt 0)

10+ Year Member



In case your directory does not contain any other directories you can delete all files in it in two words (on unix-based systems):

$dir = "/path/to/dir";
system "rm $dir/*";

lindajames

8:50 am on Aug 22, 2003 (gmt 0)

10+ Year Member



problem is i am on Windows, so that wont work. any other ideas?

myself

9:31 am on Aug 22, 2003 (gmt 0)

10+ Year Member




my $dir = "c:/path/to/dir";
foreach (<$dir/*>){
unlink;
}

timster

6:47 pm on Aug 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or, if you want a program that looks more like your orginal PHP...

#!/usr/bin/perl

opendir(temp,"/path/to/temp") or die ("Could not open temp directory");

while ($file = readdir(temp))
{

if ($file ne "." && $file ne "..")
{
unlink("/path/to/temp/$file") or warn "Failed to delete file: $file" ;
}
}
print "<font face=Verdana size=1>Done deleting files...</font>";

Courtney

6:51 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



One question...
Does your hosting come with Perl?

Because if it doesn't then their is your problem. Sorry if I didn't help at all...:(

[edited by: jatar_k at 7:00 pm (utc) on Aug. 22, 2003]
[edit reason] no urls thanks [/edit]

kelly_boyce

10:47 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



hello,

I am also using windows hosting and the code below deletes the entire contents of the directory, can someone help me convert it so that instead of deleting all the files it only deletes files that are 2 days old?

my $dir = "c:/path/to/dir";
foreach (<$dir/*>){
unlink;
}

i've been told its possible

thanx in advance.

myself

7:46 am on Aug 23, 2003 (gmt 0)

10+ Year Member



You should use perl function call() to get the time file was created or modified:

my ($modified, $created) = (stat ($filename)) [9, 10];

Times are in seconds since some day in 1970. To get current time call time():

$current = time();

So the full code will be like this:

my $current = time();
foreach my $filename (<c:/path/to/dir/*>){
unlink $filename if $current - (stat $filename) [10] > 2 * 60 * 60 * 24;
}

kelly_boyce

11:34 am on Aug 26, 2003 (gmt 0)

10+ Year Member



the following code doesnt seem to delete 2 day old files, it seems to delete more older files but not 2 day old:

my $current = time();
foreach my $filename (<c:/path/to/dir/*>){
unlink $filename if $current - (stat $filename) [10] > 2 * 60 * 60 * 24;
}

i need it to delete 2 day old files

ShawnR

11:48 am on Aug 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> 2 * 60 * 60 * 24 means greater than 2 days (i.e. 3 days or more), so for two or more, make it greater than 1 * ... instead of greater than 2 * ...