Solved

Escaping Special Chars when copying file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

22 Experts available now in Live!

Get 1:1 Help Now