Solved

Escaping Special Chars when copying file

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

19 Experts available now in Live!

Get 1:1 Help Now