
rGeoffrey
User
Mar 1, 2001, 12:53 PM
Post #2 of 4
(1158 views)
|
Starting with the silliest thing that can be wrong, are you sure that all the directories you want to delete files in are in the same directory as the script doing the deleting? And do you really want to play with a directory called 'messsege/' that has three the letter 's' three times in a row? You have a die statement in there, did it tell you anything after it ran? Also, the code looked repetitive to me so here is my rewrite...
#!/usr/bin/perl $buffer = $ENV{'QUERY_STRING'} ; print <<endHeAD; Content-type:text/html <script> /* <body> </body> */ </script> vbv endHeAD ########################################################### open (INN, "id/id.data") ; my @idfile = <INN> ; close (INN) ; my $File_start = "forum1" ; foreach $id (@idfile) { my $BoxQQ = (split (/\|/, $id))[0] ; $FileEnd2 = "-$BoxQQ.data" ; &deleteFiles ("forums/allmessges/$File_start", 'messsege/', $FileEnd2); &deleteFiles ("forums/links/links", 'links/', $FileEnd2); &deleteFiles ("forums/replays/replays", 'forums/replays/', $FileEnd2); &deleteFiles ("forums/$File_start", 'messsege_files/', $FileEnd2); my $file = "data/counters/counter$FileEnd2"; unlink $file or die "Couldn't Delete File '$file' because: $!\n"; $count++; } ########################################################### sub deleteFiles { my ($listfile, $prefix, $suffix) = @_; open (DATA, "$listfile$suffix") ; my @files = "$listfile$suffix", (map { $prefix . (split (/\|/, $_))[0] . $suffix } <DATA>) ; close (DATA) ; foreach (@files) { unlink $_ or die "Couldn't Delete File '$_' because: $!\n"; } } Each time you split a line you only want the first element, so I put a ( )[0] around the split statements to only get the one element we care about. And the bulk of the work is pushed into &deleteFiles with will read a datafile, then use it to make a list of files to delete and will also delete the datafile. Except for "data/counters/counter$FileEnd2" which is still treated differently. I also moved the semicolon on the print statement to be on the same line as print, not at the end as that is how I have always seen it and it makes me feel better. Not having directories of stuff to delete I have not tested this, but it should work if I was wrong with my first two suggestions. -- Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?
|