http://www.perlmonks.org?node_id=289676

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have one question here....
How to copy file from original directory into other directory (if the files exists), example :

/tmp/LINE1.test.txt COPY TO /bh_perl/test.txt

Otherwise, this is my script and not working...

if ( -e "$bkpdir/$month/$month-$day/*$fname*" ) { copy ("$bkpdir/$month/$month-$day/*$fname*", $procdir/$fname") or warn ("Can't copy $fname\n"), die; } elsif ( -e "$arcdir/*$fname*" ) { copy ("$arcdir/*$fname*", "$procdir/$fname") or warn ("Can't copy $fname\n"), die; } else { print ("NOT FOUND $fname in \n bkpdir/$month/$month-$day/*$fname* + \n OR $arcdir/*$fname*\n"); }

Thank you,
bh_perl

Replies are listed 'Best First'.
Re: How to copy files ?
by Limbic~Region (Chancellor) on Sep 08, 2003 at 04:52 UTC
    bh_perl,
    Take a look at File::Copy. I am not sure what all your use of * is, but I doubt it is doing what you want.

    Cheers - L~R

Re: How to copy files ?
by mandog (Curate) on Sep 08, 2003 at 05:42 UTC

    a quick look at  perldoc perlfunc or perldoc.com suggests that there is no built-in copy function in perl

    Limbic~Region wisely suggests File::Copy which is a core module. (You won't have to install it) and nicely returns (1) on success, (0) on failure and stuffs something useful int $!

    Other people tend to do stuff like:

    use strict; system('cp','/path/to/file1','path/to/file2'); my $err_num=$?>>8; die "bad copy num:$err_num\n" if $err_num;


    email: mandog
Re: How to copy files ?
by bradcathey (Prior) on Sep 08, 2003 at 12:25 UTC
    Agree with most, use File:Copy. It's as easy as:
    use File::Copy; copy (from_path_file, to_path_file) or die "error: $!\n"; #can use "move" in place of "copy", and can rename file in process
    But agree with Abigail, you need to brush up on your debugging.

      Hi,
      Thanks for your some ideas,
      After some changes here, I found that my problem is on IF statement.. they are not found any files in the particular directory although they are there...what happened on my script ? ... could some body give some ideas ?

Re: How to copy files ?
by Abigail-II (Bishop) on Sep 08, 2003 at 06:40 UTC
    Otherwise, this is my script and not working...

    Ah, one of those "not working" fans. Too lazy to indicate anything, just "is doesn't work". The man who goes to the car mechanic with "my car doesn't work". The man who goes to the doctor with "I have pain". No futher explaination. Why should he? He's going to the experts! They immediately know what's wrong. They just use ESP and know whether the problems lies in compiling, run-time errors, unexpected output, wrong assumptions what the program should be doing, or something else.

    Well, here's my answer: if the program doesn't work, you have a bug!

    Abigail

      The difference here is that, of course, car mechanics and doctors get paid for their expertise. You're right to be upset with the poster, but I thought I'd just augment your argument.
Re: How to copy files ?
by Anonymous Monk on Sep 08, 2003 at 05:04 UTC
    That doesn't compile
    String found where operator expected at crapd line 4, near "or warn (" +" (Might be a runaway multi-line "" string starting on line 3) (Missing semicolon on previous line?) Bareword found where operator expected at crapd line 4, near "or warn +("Can't" (Do you need to predeclare or?) Backslash found where operator expected at crapd line 4, near "$fname\ +" (Missing operator before \?) String found where operator expected at crapd line 5, near "} elsif ( +-e "" (Might be a runaway multi-line "" string starting on line 4) (Missing semicolon on previous line?) Scalar found where operator expected at crapd line 5, near "} elsif ( +-e "$arcdir" (Missing operator before $arcdir?) Scalar found where operator expected at crapd line 6, near "copy ("$ar +cdir" (Might be a runaway multi-line "" string starting on line 5) (Do you need to predeclare copy?) Scalar found where operator expected at crapd line 6, near "", "$procd +ir" (Missing operator before $procdir?) String found where operator expected at crapd line 7, near "or warn (" +" (Might be a runaway multi-line "" string starting on line 6) (Missing semicolon on previous line?) Bareword found where operator expected at crapd line 7, near "or warn +("Can't" (Do you need to predeclare or?) Backslash found where operator expected at crapd line 7, near "$fname\ +" (Missing operator before \?) String found where operator expected at crapd line 9, near "print ("" (Might be a runaway multi-line "" string starting on line 7) (Missing semicolon on previous line?) Bareword found where operator expected at crapd line 9, near "print (" +NOT" (Do you need to predeclare print?) Bareword found where operator expected at crapd line 9, near "$fname i +n" (Missing operator before in?) Backslash found where operator expected at crapd line 9, near "in \" (Do you need to predeclare in?) Scalar found where operator expected at crapd line 9, near "/$month/$m +onth" (Missing operator before $month?) String found where operator expected at crapd line 9, at end of line (Missing semicolon on previous line?) syntax error at crapd line 1, near "perl -c" Can't find string terminator '"' anywhere before EOF at crapd line 9.
    I suggest you fix your syntaxt errors first (you're missing a quote on the 2nd line).