Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting file script not working ... ?

Status
Not open for further replies.

davef101

MIS
Mar 3, 2006
34
GB
Heres my directory tree:

/cgi-bin
-/carts
-/Temp_ORDERS
this_script.cgi

this_script:

$directory = 'carts';
opendir(DELE, $directory) or die "Can't Open directory";
@TODELETE = readdir(DELE);
chdir($directory) or die "Can't Change directory";
foreach $file (@TODELETE) {
if (-d $file) {next};
if ((-C $file) > 1) {
unlink($file);
}
}
closedir(DELE);

$directory = '../Temp_ORDERS';
opendir(DELE, $directory) or die "Can't Open directory";
@TODELETE = readdir(DELE);
chdir($directory) or die "Can't Change directory";
foreach $file (@TODELETE) {
if (-d $file) {next};
if ((-C $file) > 1) {
unlink($file);
}
}
closedir(DELE);

chdir('../../cgi-bin');


Doesn't delete anything! .. i want it delete when files in each directory are over 1 day old ..

any help?
 
one important thing to find out first, does PERL have permission to delete files in these folders?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I would also use the glob command like so..
Code:
# get files
my @files = glob('full_path/*');

# Loop & delete
for(@files){
    if(-d $_){next;}
    if((-M $_) > 1){unlink ($_);}
}



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Yes, probably a persmissions problem because the code looks like it should work. Try this:

Code:
$directory = 'carts';
opendir(DELE, $directory) or die "Can't Open directory";
@TODELETE = readdir(DELE);
closedir(DELE);
chdir($directory) or die "Can't Change directory";
foreach $file (@TODELETE) {
   if (-d $file) {next};
   if (-C $file > 1) {
      unlink($file)[b] or print "Can't unlink $file: $!";[/b]
   }
}


$directory = '../Temp_ORDERS';
opendir(DELE, $directory) or die "Can't Open directory";
@TODELETE = readdir(DELE);
closedir(DELE);
chdir($directory) or die "Can't Change directory";
foreach $file (@TODELETE) {
   if (-d $file) {next};
   if (-C $file > 1) {
      unlink($file)[b] or print "Can't unlink $file: $!";[/b]
   }
}
chdir('../../cgi-bin');

print an HTTP header first if running from a web browser.

- Kevin, perl coder unexceptional!
 
Thanks 1DMF that works fine now ..

Tell me, what is the 'glob' statement actually doing, just so i fully understand.
 
from perlfunc:

glob EXPR
glob

In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix shell /bin/csh would do. In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted. This is the internal function implementing the <*.c> operator, but you can use it directly. If EXPR is omitted, $_ is used. The <*.c> operator is discussed in more detail in "I/O Operators" in perlop.

Beginning with v5.6.0, this operator is implemented using the standard File::Glob extension. See File::Glob for details.

It's rougly the equivalent of
Code:
opendir (DIR, "./directory");
my @files = readdir(DIR);
closedir (DIR);

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top