Reply to topic
Perl File::Copy problems
paul28


Joined: 11 Jul 2005
Posts: 6
Reply with quote
Hi all, i'm trying to copy a list of files from one location to another. But after perl does the work of reading the paths from the list, the copy command doesn't work. weird thing is, when i have perl print the paths to the screen, and i manually copy and paste one of them directly into the copy command, it works. Whats going on?

Code:

use strict;
use File::Copy;

open( IN, "/Users/paul/Desktop/missinglist" );

my $line;
my @line_array;
while ( $line = <IN> )
    {
     push(@line_array , split(/\r/, $line ));
    }
my $num = @line_array;
my $count = 0;
my $patha;
my $pathb;
my @array;

while ($count < $num) {
   my @array = split(/\t/, @line_array[$count] );
   my $patha = @array[0];               #this is the first path ex. "/Music/iTunes/iTunes Music/Silbermond/Verschwende deine Zeit/17 Symphonie (On Stage).m4a"
   $count++;

   my @array = split(/\t/, @line_array[$count] );
   my $pathb = @array[0];               #this is the second path ex. "/Users/paul/Desktop/itunescopy/17 Symphonie (On Stage).m4a"
   $count++;

   print "-$patha-\t-$pathb-\n";
   copy($patha, $pathb);                   #this should copy, but doesn't - when the paths are directly typed in here w/ quotes, it works
}
cheryl


Joined: 17 Apr 2004
Posts: 84
Location: Newark, DE
Reply with quote
Are you trying to run the perl script from the web or just from the command prompt? If you are running the perl script from the internet, it is possible that the directory you are trying to write to does not have permissions to do so. If you are running it from the command prompt, what is the error that you are getting?
paul28


Joined: 11 Jul 2005
Posts: 6
Reply with quote
Thats the thing, there is no error, it continues comparing file names, and goes through that copy command as if it did it, only it didnt actually copy anyhitng. i think it has something to do with the $patha string being weird somehow. dont know. And yes, this is only local on the command prompt.
cheryl


Joined: 17 Apr 2004
Posts: 84
Location: Newark, DE
Reply with quote
I believe the situation is that you need to remove the whitespace from the end of the filename of $patha - at least, that is what I needed to do to get the code to work when I tested it out. I trimmed the whitespace by using the split function:
my @tmpPath = split(/\s+/, $patha);
then reset $patha back to $tmpPath[0]

I would recommend doing the same for $pathb.

I hope this helps!
paul28


Joined: 11 Jul 2005
Posts: 6
Reply with quote
Thank you, i'll try it out.
paul28


Joined: 11 Jul 2005
Posts: 6
Reply with quote
no, this doesn't quite work because i have folders with multiple words, and taking [0] of that new array only takes the path until the first space. i'm not sure how to do it, but i need everything but the last [$]. I'll try again.

i get this now:
/Users/paul/Music/iTunes/iTunes

i need this (and only this):
/Users/paul/Music/iTunes/iTunes Music/50 Cent/Unknown Album/Get Rich or Die Tryin.mp3

i think it must be something like this white space thing, but when i print "-$patha-" the dashes are directly on either side. so doesn't that mean that there isn't any trailing whitespace?
cheryl


Joined: 17 Apr 2004
Posts: 84
Location: Newark, DE
Reply with quote
It does sound like it...can you post a sample of the file you are reading in so I can make sure I'm using the same format? Also, when you run the copy function, maybe try doing:
copy ($patha, $pathb) or die "copy failed: $!";
That should at least show you why the copy failed - that's how I came across the spaces thing.
paul28


Joined: 11 Jul 2005
Posts: 6
Reply with quote
Ok, my project has taken a bit of a turn. Let me explain. From the begginning, i had a pl script that compared two lists of songs from iTunes. In this script, when it found one that the other list didnt have, it was suppoesed to send copy it to another central location for importing to the other iTunes. Problem was, that copy command never works, except when i copied it manually. So i tried to have the computer copy it so to speak. it was a bit of a work around. I wrote this second script, the one i showed here, which was suppoesed to copy songs from a list that the first script created. Problem was, this didn't work either. Thats when i posted here. In the mean time, i can up with another workaround that worked. I had the first script compare and create a path list - in a very specific format. Then i manually copied the entire list at once, and pasted it into a list variable in an applescript script, which conveniently did the copying. So, as i see it now, im scraching the script i posted here, it was a workaround that didnt work. I'll post the origional pl script, although now set up to create the file for applescript to use. It would still be great to get perl to do the copying itself, but not really necessary anymore. From this script you can see how the old path list was created. I think it was:

"$path\t \n"

the purpose of this was to split each line at the tab, therby leaving the return in the unused half, and not having to deal with it. Anyway, my question is now, why wouldn't it copy($patha, $pathb) in this script to start with? Please note that the format of the paths is for applescript now, but you can figure out what it means. : instead of /, and you have to state the drive instead of just / Thanks for your help!

Code:

#use diagnostics -verbose;
use strict;
use File::Copy;

my $start_time = time();

my $from = $ARGV[0];
my $to = $ARGV[1];
my $fast = $ARGV[2];

print "\n\tComparing...\n";


my ( $linea, @headersa, @columnsa );
my ( $lineb, @headersb, @columnsb );
open( INA, $from );
open( INB, $to );
open( OUT, ">/Users/paul/Desktop/missinglist" );

my @line_arraya;
while ( $linea = <INA> )
    {
     push(@line_arraya , split(/\r/, $linea ));
    }

my @line_arrayb;
while ( $lineb = <INB> )
    {
     push(@line_arrayb , split(/\r/, $lineb ));
    }

my $percent = 0;
my $percent_last = 0;
my $mod_count = 0;
my $file_count = 0;
my $fullcount = 0;
my $halfcount = 0;
my $num_b = @line_arrayb;
my $num_a = @line_arraya;
foreach $linea (@line_arraya)
    {
   @columnsa = split( /\t/, $linea );
   my $name = $columnsa[0];
   my $artist = $columnsa[1];
   my $album = $columnsa[3];
   my $path = $columnsa[24];
   my $mark = 0;
   my $count = 0;
   OVER: foreach $lineb (@line_arrayb)
       {
      @columnsb = split( /\t/, $lineb );
   
      my $name2 = $columnsb[0];
      my $artist2 = $columnsb[1];
      my $album2 = $columnsb[3];
#print "$name - $artist - $album\t$name2 - $artist2 - $album2\n";
      #my $path2 = $columnsb[24];
      if ($fullcount != 0)
          {
#print "$name - $artist - $album\t$name2 - $artist2 - $album2\n";

         if (($name eq $name2) && ($artist eq $artist2) && ($album eq $album2))
                 {
            $mark = 1;
#print "$mark\n";
            $count++;   
            $file_count++;
            last OVER; #drop out of this loop if match is already found
                 }
         else
              {
            $count++;
            if ( ($count) == ($num_b) && $mark == 0)
                     {
               $mod_count++;
               my @parsedpath = split( /\:/, $path );
               my $ppc = @parsedpath;
               my $patha = 'Fesplatte:Users:paul';
               my $part;
               my $counta = 0;
               foreach $part (@parsedpath)
                   {
                  if ($counta >= 3 && $counta < $ppc )
                     {
                     $patha = $patha . ':' . $part;
                     }
                  $counta++;
                   }
               my $patha = '"' . $patha . '"';
               #my $pathb = '"Festplatte:Users:paul:Desktop:itunescopy"';
               print OUT "$patha, ";
               print "||||||||||||$name - $artist\n";

               #my $finder = '"Finder"';
               #my $arg = "osascript -e 'tell app $finder to move $patha to $pathb'";
               #print "$arg\n";
               #system($arg);

   
               $file_count++;


                    }
              }
#print "$mark\n";

          }
          $halfcount++;
       }
    $fullcount++;
   $percent_last = $percent;
   $percent = int( ( $fullcount / $num_a ) * 100 );
   if ($percent != $percent_last)
       {
      print "\t$percent% Complete.\n";
       }
    }

my $sec_label = 0;
my $end_time = time();
my $total_time = abs($end_time - $start_time);

if ($total_time == 1)
    {
   $sec_label = "second";
    }
else
    {
   $sec_label = "seconds";
    }

my $list1 = ($num_a-1);
my $list2 = ($num_b-1);

print "\n Songs in first list: $list1\n";
print "Songs in second list: $list2\n";
print "      Songs copied: $mod_count\n";

print "\n\tTotal Time: $total_time $sec_label.\n\n";


close( INA );
close( INB );
Try this trick, it worked for me!
uma


Joined: 14 Jul 2005
Posts: 1
Reply with quote
Hi everybody,
I am a newby to Perl programming!

I was trying to copy files from one directory to another and had the same problems as you!
My colleague pointed out that I can use:
system("cp $fileName ./destinatiodir/$fileName");

This definetly works!

Same rule applies to mkdri, rm, mv etc.......!

Good Luck

-U
paul28


Joined: 11 Jul 2005
Posts: 6
Reply with quote
Thanks, but that doesn't help me, because my path variable was sumehow corrupted so to speak from the beginning, I couldn't just type the path in, it had to get pulled out of a file and reformatted, the problem was there. But thanks. You said your colleague, out of curiosity, what do you do for a living? I'm a senior in high school, and I'm just curious what there is out there that includes some casual programming but thats not the main job.
Perl File::Copy problems
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic