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
tmccar10Asked:
Who is Participating?
 
maneshrConnect With a Mentor Commented:
i would insert the foll code snippet in your script and try...


Main:
{
  print header;

  $dellist="../data/filedel.txt";        #create scalar value of file
  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;

  #delete files whose names appear on the list
  ##NOTE: unlink doesn't report which filenames it couldn't delete, only how many it did delete.
  unlink(@deller) == @deller  or die "Couldn't unlink all of @deller: $!\n";
  exit;
}
0
 
geotigerCommented:
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



0
 
maneshrCommented:
can you post your current code pl.??
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
tmccar10Author Commented:
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;      
}
0
 
maneshrCommented:
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.
0
 
tmccar10Author Commented:
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
0
 
maneshrCommented:
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.      
0
 
tmccar10Author Commented:
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    
0
 
ozoCommented:
geotiger's method should do that for you, although you probably want to chomp @a;
0
 
tmccar10Author Commented:
Edited text of question.
0
 
ozoCommented:
#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 $!";
0
 
darinwCommented:
Reviewing question.
 
darinw
Customer Service
0
 
tmccar10Author Commented:
Submitted another question to provide "ozo" with split of 100 points
0
All Courses

From novice to tech pro — start learning today.