Link to home
Start Free TrialLog in
Avatar of ianberns
ianberns

asked on

Single file copy

I would like to copy a single image file (.jpg) from one directory to another directory with a different file name (same extension).

My source perl program is located in: public/cgi-bin/
My source image directory is located: public/source/dir1/
My destination directory is located: public/source/

I have already tried the File::Copy and works great with my active perl debugger, but does not work on the server, so I would like to use a system command in Perl to copy this file.
Avatar of ianberns
ianberns

ASKER

Adjusted points to 40
ASKER CERTIFIED SOLUTION
Avatar of b2pi
b2pi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The File::Copy fails in two ways.

My simple Perl program looks like the following:

#!/usr/bin/perl5

use File::Copy;

$sourcefile = "../source/dir1/image1.jpg";
$destinationfile = "../source/dest.jpg";
copy ($sourcefile, $destinationfile);

exit;

1st error:
use File::Copy;
  (the syntax error is NEXT TWO TOKENS 'use File' )

2nd error
copy ($sourcefile, $destinationfile);
  (the syntax error is NEXT TWO TOKENS 'copy (' )


If using a 'system' command, syntaxwise, how would one write the copy command if both source and destination path/filenames were given in variables?
what is the output of

/usr/bin/perl5 -v


/usr/bin/perl5 -v

its output when executed:

This is perl, version 5.003_02
        + two suidperl security patches

Copyright 1987-1996, Larry Wall

Wrong perl version maybe for the File::Copy  function?
This isn't making sense (Although I'd recommend upgrading your perl in
any case)

You say if you run it from the command line, you get those parse
errors? Wait a minute... those aren't perl error messages, those are
shell errors...

Are you sure your shebang (#!) line is proper?  What happens if,
assuming your script name is perlcopy.pl, you run

%/usr/bin/perl5 -w perlcopy.pl

instead of

#perlcopy.pl

??
/usr/bin/perl5 -v

its output when executed:

This is perl, version 5.003_02
        + two suidperl security patches

Copyright 1987-1996, Larry Wall

Wrong perl version maybe for the File::Copy  function?
Avatar of ozo
Those look more like perl4 errors.  (assuming you paraphrased the error messages slightly)
Do you get the same error running from the command line?
What do you get if you run

#!/usr/bin/perl5
print $];

on the server?
Found my problem:

1.  File::Copy [ie.  copy($newdir, $destination_dir) ]  does work, it helps if you have 'write' access to the directory you're writing to (a simple chmod fix).

2.  system (test$); where test$ = "/bin/cp $newdir $destination_dir"; also works!
 note: $newdir, and $destination_dir are simple path/filename string variables for flexibility.

Lost some points here!  Anyhow  'b2pi' thanks for the help.....

Thanks b2pi, I'll have more idiot questions
> 1. File::Copy [ie.copy($newdir, $destination_dir) ]does work, it
 >    helps if you have 'write' access to the directory you're writing to
 >    (a simple chmod fix).

Yes, that's true, but it doesn't explain the error messages you were
getting.

 > 2.system (test$); where test$ = "/bin/cp $newdir $destination_dir";
 >   also works!

For even more flexibility, you might try

my($copy) = '/bin/cp';

then, you can do

system("$copy $newdir $destination_dir");


 system($copy,$newdir,$destination_dir);
should be less likely to do nasty things if you have shell metacharacters in $newdir or $destination_dir
but
  use File::Copy;
should be more flexible in working no matter what cp is named on your system