jueves, 26 de enero de 2012

find a file and copy to another location

Well having a list of names of files I have to find it inside a root directory. If it is not there I have to look in all the directories inside of root dir until I find it or I finish the search.
Then I have to copy this file inside another directory where i'm keeping all the files of the list. My problem was with the method File::Copy because once a file was locate when i'm copying the result is a empty file. So for that I decided to do with bash commands inside my perl script. See the code below:

#!/usr/bin/perl

use strict;
use File::Find;
use File::Copy;
use File::Path;
use warnings;
#leer el archivo con la lista
open(FILE,"/home/.../Documentos/list.txt");
#leer una linea a linea y guardar en un array
my @lines = <FILE>;     # Read it into an array
close(INFO);            # Close the file
print @lines;
close(FILE);
#dividir la linea en tres porque necesito añadir caracteres
for my $line(@lines){
chomp($line);
my $number;
my $code;
if(length($line)==11){
$number=substr($line,2,7);
$code=substr($line,9,11);
}
else{
$number=substr($line,2,6);
$code=substr($line,8,10);
}
#print $number."\n";
#print $code."\n";
#buscar el archivo dentro del directorio

my $dir = '/root/dir/....';

find(\&imprimir_archivo_pl, $dir);

sub imprimir_archivo_pl{
my $elemento = $_;
my $nomFile="EP-".$number."-".$code.".xml";
if(-f $elemento && $elemento eq $nomFile){
my $original_file = "$File::Find::name";
my $path = '/home/..../Documentos/Copies/'.$nomFile;
#copy with shell commands
system("cp -R -pi $original_file $path") == 0
    or die "external cp command status was $?";
}

}
}

problem with cpan

when I want to install something in cpan on ubuntu, he said me that I haven't install YAML. I tried to install YAML via cpan but I got a Error. So looking for somewhere I find my solution:
first  update your system:
  • apt-get update
  • apt-get upgrade
install YAML via synaptic package manager:
  • libconfig-yaml-perl
now update cpan lib's
  • cpan -r
and now no more errors ..yeahhhh :P