Solved

Single file copy

Posted on 1998-09-15
12
227 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now