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

x
?
Solved

PERL Copy command not working for me

Posted on 2009-04-10
5
Medium Priority
?
1,010 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

864 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