Solved

Escaping Special Chars when copying file

Posted on 2010-08-17
2
622 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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