Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Escaping Special Chars when copying file

Posted on 2010-08-17
2
Medium Priority
?
628 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 85

Accepted Solution

by:
ozo earned 2000 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

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!

Question has a verified solution.

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

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…
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…
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

877 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