Solved

Escaping Special Chars when copying file

Posted on 2010-08-17
2
623 Views
Last Modified: 2012-08-14
Hi,

I'm having trouble copying a file from folder a to folder b. I don't want to alter the filename in any way.

Example Test Filename:
this $is %a @screwed 'filename.(2).&test

File location is /tmp

From console I can do

cp /tmp/this\ \$is\ %a\ \@screwed\ \'filename.\(2\).\&test /test/this\ \$is\ %a\ \@screwed\ \'filename.\(2\).\&test

which works

I cant find a way to escape the characters correctly for File::Copy or `cp $source $destination` to work i keep getting errors.

Using Net::Scp I can copy this file to another server using quotemeta as per code snippet.

How would I do the same to copy the file from one folder to another locally?

quotemeta seems to escape everything where as the console only escapes what needs escaping?





use Net::SCP;
chdir("/tmp");
$scp = Net::SCP->new( "remoteserver", "root" );
@files= `find /tmp -type f -printf %p\\|%s\\\\n`;
foreach my $file (@files){
        chomp($file);
        ($file, $size) = split(/\|/, $file);
        $remote_file = quotemeta($file);
        $scp->put("$file", "$remote_file") or die $scp->{errstr};
        exit;
}
$scp->quit();
exit;

Open in new window

0
Comment
Question by:APNFSSC
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 33460451
I would expect  quotemeta to work for `cp $source $destination` since that being passed through a command interpreter
but no escaping should be needed for  File::Copy
0
 
LVL 10

Author Comment

by:APNFSSC
ID: 33460482
Hi ozo,

Thanks for your response.

I tried loads of stuff except for the obvious it seems...

Thanks for the advice.
foreach my $file (@files){
        chomp($file);
        ($file, $size) = split(/\|/, $file);
        $just_file = $file;
        $just_file =~ s/\/tmp\///;
        copy("$file","/test/$just_file") or die "Copy failed: $!";
        exit;
}

Open in new window

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

808 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