Link to home
Start Free TrialLog in
Avatar of saloua
saloua

asked on

perl /unix/directories

Hi,

Can you please tell me how in unix I can :
-open a directory and then read each file by using perl?( the file does not have any extension!!)
-copy an image in a directories
Thanks a lot!
Avatar of rag2000
rag2000

you can try.....

# to go to the directory
chdir($dir);

# to get a list of files from that directory
$filelist=`ls *.*`;
@filelist=split(/\s/,$filelist);

foreach $filelist (@filelist){
  open(FILE,"/full/path/$filelist");
  @filecontent=<FILE>;
  close(FILE);

  ... do what u want ....
}
saloua,

"...-open a directory and then read each file by using perl?( the file does not have any extension!!)...."

here is a very portable script that accepts as many parameters as you pass.

it checks if each parameter is a directory. if the parameter is a directory, then it lists all files in it.

===my_dir.pl
!/usr/local/bin/perl

if (scalar (@ARGV) <=0){
  print "Usage: $0 <dir 1> <dir 2> ... <dir n>\n";
  exit;
}

foreach $dir (@ARGV){
  if (-d $dir){
    opendir(DIR,$dir) || die $!;

    ##  Directory listing wihtout . and ..
    @allfiles=grep !/^\.\.?$/, readdir DIR;

    foreach(@allfiles){ ## Process entry by entry
      $file=$dir."/".$_;

      if (-f $file){    ##  Yes its a plain file
        print $file,"\n";
      }
    }
    close(DIR);
  }else{
   print $dir," is not a directory!\n";
  }
}

"...-copy an image in a directories ...."

is this in any way related to the first question?

What is it that you are trying to do here?

please explain in more detail.
Avatar of saloua

ASKER

Hi,

Thank you for your help,

Here are more details about the image part:

In each file that I am opening I will find something like:
http://www.myserver_name/location_of_my_image/image_name.jpg
I would like to copy this image into an other dir.
I hope this will help you to help me:)
Thanks
Avatar of saloua

ASKER

Hi,

Thank you for your help,

Here are more details about the image part:

In each file that I am opening I will find something like:
http://www.myserver_name/location_of_my_image/image_name.jpg
I would like to copy this image into an other dir.
I hope this will help you to help me:)
Thanks
As far as I have understood ur question :

1>U need to read all files in a directory.
2>All these files has some link, something like : http://www.myserver_name/location_of_my_image/image_name.jpg 
3>U want to connect to this site, get the Image and save it in another directory on ur system.

Let me know if I am right, I will post the working solution of this.

thanx.
Avatar of saloua

ASKER

Yes it's exaclty what I need to do, I will really appreciate your Help.
By the way, I was able to read the file by using maneshr  code!
Thank You
saloua,

".. I was able to read the file by using maneshr  code! .."

glad to know that the first part of your problem was solved.

"...In each file that I am opening I will find something like:
                      http://www.myserver_name/location_of_my_image/image_name.jpg ..."

given the above, you dont even have to go for a very complex PERL script.

A simple combination of UNIX commands should do the work for you.

To use the command, go to the directory where you want the search to start and then issue the following command

find . -type f -name "*.htm?" -exec grep ".jpg" {} \;

The above command would recursively search for files which fits the pattern *.htm?

in each of these files, the grep command will search for the string .jpg.

once you have the output with you, you can redirect it and store it in a file.

This file can then be used as the input for a PERL script. This PERL script would do the actual copying of the images.

Hope that helps.
saloua,

Did you get the solution you were looking for?

What solution, if any, did you use?

Please let us know.
saloua,

did you get the solution you were looking for?

What solution, if any, did you use?

let us know
saloua,

Try this :

------------------------------------------------------
#!/usr/bin/perl

use LWP::Simple;

if(scalar(@ARGV)<=1) {
die "Usage : $0 <Input Directory> <Output Directory> \n";
}

$inputDir=$ARGV[0];
$outputDir=$ARGV[1];

die "Invalid Input Directory : $!\n" unless (-d $inputDir) ;
die "Invalid Output Directory : $!\n" unless (-d $outputDir) ;

opendir(DIR,"$inputDir") || die "Error :$!\n";
while($file = readdir(DIR)) {
     next if ($file =~ /^(\.\.?)$/);
     if (-f "$inputDir\/$file") {
          open(FILE,"$inputDir\/$file");
          print STDOUT "Reading file $file.... \n";
          while(<FILE>) {
               if(/(http\:\/\/[\S]+)/) {
                    $url=$1;
                    if($url=~/(jpg|jpeg|gif)$/i) {
                         $imageUrl=$url;
                         ($img)=($url=~/\/([^\/]+)$/);
                         print STDOUT "Fetching image $imageUrl.... \n";          
                         if($content=get($imageUrl)) {
                              open(IMG,">$outputDir\/$img") or die "Could not open output file :$!\n";
                              binmode(IMG);
                              print IMG $content;
                              close(IMG);
                         } else {print STDOUT "Cannot fetch $imageUrl : $!\n"}
                    }
               }
          }
          close(FILE);
     }
}
closedir(DIR);

--------------------------------------------------

This will read all the files in the input directory, fetch the images from the urls given in the files and save it in the output directory.
Make sure u have LWP::Simple module installed.
see perldoc LWP::Simple
If u do not see the documentation, u will need to install it.

Let me know if this helps.


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
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange