[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

unlink/file list

Posted on 2000-05-12
13
Medium Priority
?
991 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:tmccar10
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 12

Expert Comment

by:geotiger
ID: 2805501
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2805509
can you post your current code pl.??
0
 

Author Comment

by:tmccar10
ID: 2805684
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Expert Comment

by:maneshr
ID: 2805853
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
 

Author Comment

by:tmccar10
ID: 2807648
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2810754
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
 

Author Comment

by:tmccar10
ID: 2811599
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
 
LVL 85

Expert Comment

by:ozo
ID: 2811791
geotiger's method should do that for you, although you probably want to chomp @a;
0
 

Author Comment

by:tmccar10
ID: 2821340
Edited text of question.
0
 
LVL 16

Accepted Solution

by:
maneshr earned 200 total points
ID: 2821516
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
 
LVL 85

Expert Comment

by:ozo
ID: 2821920
#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
 
LVL 3

Expert Comment

by:darinw
ID: 2826241
Reviewing question.
 
darinw
Customer Service
0
 

Author Comment

by:tmccar10
ID: 2826681
Submitted another question to provide "ozo" with split of 100 points
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question