Link to home
Start Free TrialLog in
Avatar of Thom McCarthy
Thom McCarthyFlag for United States of America

asked on

unlink/file list

I think I'll give this one more try. I think I've gotten close. But I'm still not able to remove the files from the "docs" directory.
The "data/filedel.txt" gets stripped of files by running the procedure below. That's not what I'm shooting for.
I can print @deller after the "opendir", so apparently the list exists. I just don't know what's wrong/missing.

Thanks...again
Snippet:>>>>>>>
#!/usr/bin/perl
use CGI qw(:standard :shortcuts center);
Main:
{
    print header;

    open(TXT, "<../data/filedel.txt")||die("Can't openTXT\n");                        # Reads from this file to get files to delete
                                                                                                                   #file created from previously run procedure--(file name including ext--comma delimited)
   
     $dellist="../data/filedel.txt";                                                                    #create scalar value of file                  
           close (TXT);
    open DELL,"<$dellist" or die "Can't open DELL\n";                          
           @deller=<DELL>;                                                                             #Convert to LIST
           close (DELL);

                                   

   unless(opendir DOCS,"../docs/")                                                            #open directory containing files to "delete"
      {          
        print center("Cannot Find Directory");
        exit;                              
      }  

      
    closedir DOCS;

      unlink @deller;                                                                                      #delete files whose names appear on the list
     
exit;      
}


<<<<<<<<<<<<<<<<<<<<<<<ORIGINAL QUESTION<<<<<<<<<<<<<<<<<<<<<<<
<I need to remove files regularly.

<I put together a process that identifies the files (reading a <substring of the name) and put the complete file name into a <seperate file.

<Can I make a LIST from this file so that I can "unlink" the <files that are named in it? Should I comma delimit the <individual file names? does the way I'm doing it make any sense?

<I suspect that this is a very simple question and I'd be more <embarrassed, if I weren't running out of time
Avatar of geotiger
geotiger

If you put each file in a line, you can open the file and then unlink one by one.

$fn = "/path/to/my/file";

open FILE, "<$fn" or die "could not open $fn:$!\n";
while (<FILE>) { unlink $_; }
close(FILE);

Or

open FILE, "<$fn" or die "could not open $fn:$!\n";
@a = <FILE>;
close(FILE);
unlink @a;   # unlink all the file at once



can you post your current code pl.??
Avatar of Thom McCarthy

ASKER

There's an html process to grab the $mo scalar--didn't include. The html here is part of the development process




#!/usr/bin/perl

use CGI qw(:standard :shortcuts center);


Main:
{
    print header;


    open(TXT, ">../data/filedel.txt")||die("Can't open file1.txt \n");
    unless(opendir DOCS,"../docs/")
      {          
        print center("Cannot Find Directory");
        exit;                              
      }                                        


      @files=sort grep !/^\.\.?$/,  readdir DOCS;
      
      
    closedir DOCS;
    import_names;
    $thisdate='date';
    chop($date = `date +" %m/%d/%y "`);
    print start_html;
    print "<body background=../images/xtrab.gif>";
    print "<center> <img src=../images/xtra.gif></center><BR>";

     
 
    print <<TABLETOP;    
<center><table width=90% border=0 bgcolor=#E4DEDE >
           
TABLETOP

            print <<ROW;
<tr><td valign=middle align=center colspan=3 ><font size=+3 color=blue>Available Updates</font></td></td></tr>
ROW

    foreach  (@files) {
   
     $filename = $_;
     ($root,$name1)=split /:/,$filename;
     ($name,$ext)=split /\./,$name1;
          $yr=substr ($root,0,2);
              $mo=substr ($root,2,2);
              $dy=substr ($root,4,2);
              $root1=($mo.$dy.$yr);

          if($Q::start==$mo){      
                                  
          print TXT $_."," ;    
   
                  print <<ROW;

<tr><td align=center valign=middle> <font size=+2> $mo</td><td><a href="../docs/$filename"> $_</font></a> </td></tr>
ROW
               
             }
            }

    print <<TABLEBOTTOM;                      
</table></center>
TABLEBOTTOM


 print "<center><br><br> Return to:<br><a href=../../> [ NEWS ]</a> X<a href=../../../../> [ N A PS &amp;L Home ]</a>O <a href=../../../../../> [ PS &amp;L Home ]</a> </center>";
    print"</body>";
       print end_html;
       


                              


exit;      
}
tmccar10,

ok! so now let me explain what it is that you are doing & what is it that you want to do.

you are basically reading the ../docs directory, after removing. and .., and storing the results in an array (@files).

next you do a foreach on that array of file.

now you also want to unlink/delete the file within this loop, right??

Eg.
foreach(@files){
##do all your processing

##delete the file
}

pl. confirm the same, so that i can modify your code to work as per your requirements.
I think I'm dealing with this like an old database process. I was trying to identify the "records" (filenames, in this case), once I identified them, I put them into a flat file.
The way I understood UNLINK, I needed a LIST of the file names to delete them.
The process I made was to accomplish the first part, create the file.
I thought what I needed to do then was to create the LIST and then do the UNLINK function.
Apparently, I got the process wrong
i think what you should be doing is using opendir/readdir to read the directory and store the list of files in that directory in an array.

next you loop through/iterate through that array and use the unlink sub routine to delete that file after checking if delete is allowed.

you have already done the first part of it. the foreach statement that i provided above kind of starts off the 2nd step.

let me know if you would like to have the entire part.

i could write a sample script that will accept a directory name as a parameter and delete all files in that directory.      
I think I'm confusing folks.

I have a directory that contains a bunch of files. Each file is used as an item in a daily update news page.

I want to be able to delete files as they become obsolete. I want to maintain the other files in the directory, add new ones and delete others as needed. I'm having trouble removing  the obsolete files.

I got the names of all the files I wanted to remove into a seperate file. Then I wanted to create a LIST from that file that I could use against the original directory with UNLINK, which I understood needed a LIST of filenames in order to remove the files from the original directory    
Avatar of ozo
geotiger's method should do that for you, although you probably want to chomp @a;
Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of maneshr
maneshr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#file created from previously run procedure--(file name including ext--comma delimited) filedel.txt
 chomp @deller;
 @deller = map{split/\s*,\s*/} @deller;
 chdir "../docs/" or warn "Cannot Find Directory $!";
Reviewing question.
 
darinw
Customer Service
Submitted another question to provide "ozo" with split of 100 points