Im trying to copy a file to a new directory, the file name is in a variable ($file). What am i doing worng??:
I've tried these:
system "cp $file ./scratch/file.out";
system "cp `$file` ./scratch/file.out";
help please...
The first system command should have worked I think.. but maybe backticks will have more success.
Also as you are using a relative path are you sure you have permissions to write to scratch/file.out and that a scratch/file.out exists below the directory where your script is located?
Maybe better to use the absolute path? Something like (depending on your config):
Code:
system "cp $file /tmp/scratch/file.out";
The Perl Cookbook Recipe 9.3 "Copying or Moving a File" suggests using the File::Copy module - you can get the perldoc documentation by doing "perldoc File::Copy". *Or*, the other option it suggests is using the "system" command as you've tried to do - here's how it suggests using system:
system("cp $oldfile $newfile" ### unix
The Programming Perl 2nd edition p.230 describes using the "system" command - here's what it suggests:
@args = ("command", "arg1", "arg2"
system(@args) == 0
or die "system @args failed: $?";
So, following that, I would do something like this for your cp command:
@args = ("cp", "$oldfile", "$newfile"
system(@args) == 0
or die "system @args failed: $?";
Remember that, if you're executing the command from a cgi script, your current directory is your cgi directory. So unless the file you're trying to copy is in your cgi directory you'll need to include a path to that too. It's usually safer to use absolute paths in a cgi script. Tracy Dryden
tracy@bydisn.com
I second Tracy's comment - whenever possible I *always* use absolute paths with filenames, whether I'm writing cgi scripts or just plain perl scripts, and I rarely(never say never) have problems.
Hardy Merrill
Mission Critical Linux, Inc.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.