Solved

Single file copy

Posted on 1998-09-15
12
247 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:ianberns
[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
  • 6
  • 4
  • 2
12 Comments
 

Author Comment

by:ianberns
ID: 1204858
Adjusted points to 40
0
 
LVL 5

Accepted Solution

by:
b2pi earned 40 total points
ID: 1204859
system('/bin/cp /public/source/dir1/source.jpg /public/source/dest.img');

- or -

`/bin/cp /public/source/dir1/source.jpg /public/source/dest.img`

- or -

qx { /bin/cp /public/source/dir1/source.jpg /public/source/dest.img};

## If you're on win32, use copy rather than cp

A more interesting question is why File::Copy doesn't work. What error
message do you get?

[Assuming you're already set up for HTML output]

use File::Copy;

copy("/public/source/dir1/source.jpg" "/public/source/dest.img")
    || die "File::Copy failed: $!";

0
 

Author Comment

by:ianberns
ID: 1204860
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:b2pi
ID: 1204861
what is the output of

/usr/bin/perl5 -v


0
 

Author Comment

by:ianberns
ID: 1204862
/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?
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204863
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

??
0
 

Author Comment

by:ianberns
ID: 1204864
/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?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204865
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?
0
 

Author Comment

by:ianberns
ID: 1204866
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.....

0
 

Author Comment

by:ianberns
ID: 1204867
Thanks b2pi, I'll have more idiot questions
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204868
> 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");


0
 
LVL 84

Expert Comment

by:ozo
ID: 1204869
 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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

691 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