Solved

Single file copy

Posted on 1998-09-15
12
209 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 184
perl rename 2 137
ppm conversion to curl on a module install 8 74
batch script for automated email 12 94
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

16 Experts available now in Live!

Get 1:1 Help Now