Solved

PERL Copy command not working for me

Posted on 2009-04-10
5
965 Views
Last Modified: 2012-05-06
I am trying to copy one file from one directory to another..

use File::Copy;
use File::Path;
copy("begperl/MODE3.txt","BASELINE/MODE4.txt"); but for some reason it's not calling an error and it is not copying the file either :'( i also tried this:
 
sub copy
{ my($from, $to) = @_;
  if(open(IN, $from))
  {  if(open(OUT, ">$to"))
     {  while(read IN, $buf, 4096)
        {   print OUT $buf;
        }
        close IN;
        close OUT;
        }
     close IN;
  }
}
 
&copy("/begperl/MODE3.txt","/BASELINE/MODE4.txt");
 
and nothing :'( do you know where my mistake could be? thankssssssss :) you're awesome :)

Open in new window

0
Comment
Question by:cucugirl
5 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 24118842
You should display the error when it doesn't succeed:
    copy("begperl/MODE3.txt","BASELINE/MODE4.txt") or die "Could not copy: $!\n";

Or when you open:
    if(open(IN, $from)) {
       ...
    }
    else {
        die "Could not open input: $!\n";
    }

0
 

Author Comment

by:cucugirl
ID: 24118897
Could not copy: No such file or directory
it's weird though because the file exists.. M:\begperl and M:\BASELINE
im trying to copy the file from begperl to BASELINE

thanks! for that tip :)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24119444
That file may exist, but you have specified a relative path rather than an absolute path.

Use

copy "M:/begperl/MODE3.txt","M:/BASELINE/MODE4.txt" or die "Could not copy MODE3.txt because $!\n";
0
 
LVL 13

Expert Comment

by:marchent
ID: 24172108
Use the windows copy command directly using function system();

system("copy M:/begperl/MODE3.txt M:/BASELINE/MODE4.txt");
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 125 total points
ID: 24172293
marchent>>Use the windows copy command directly using function system()

Doing this, you don't know if the command is successful.  It'd be better to fail and know it than to fail and not know it.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl strange behaviour 5 72
Perl output collect 7 67
instmodsh - listing perl modules 2 79
read an xml file in perl 2 32
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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