Solved

PERL Copy command not working for me

Posted on 2009-04-10
5
973 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filtering a file to table 9 102
Linux rename files by reordering dates in the file name 6 39
Insert Text into odbc.ini file 15 71
parse a file and get data out 11 110
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

726 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