Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

how to copy files with spaces in name

by ric.techow (Initiate)
on Mar 06, 2009 at 09:37 UTC ( #748795=perlquestion: print w/replies, xml ) Need Help??
ric.techow has asked for the wisdom of the Perl Monks concerning the following question:

I am reading files from a unix directory and want to copy to another as a backup. problem is that file names with spaces cause this line to fail. $status = copy ("$groupFile","$errDir"); $groupFile contains a fully qualified file name. $errDir is a directory. Fine for normal files but not those with spaces. I have seen examples where a known file name can have any spaces escaped. But I don't know what these files are (and don't care). Do I need to check for spaces and change every space in $groupfile to '\ '? Is there a better way?? ( using File::Copy;)

Replies are listed 'Best First'.
Re: how to copy files with spaces in name
by rovf (Priest) on Mar 06, 2009 at 10:03 UTC
    Works for me:

    mucn13154:~/tmp 1 502 $ touch "has space" mucn13154:~/tmp 1 503 $ perl -lwe 'use File::Copy qw(copy); copy("has +space","has more space")' mucn13154:~/tmp 1 504 $ find . -name 'has*' ./has space ./has more space
    If you try my example on your perl, what error message do you get? Which version of Perl, respectively File::Copy, are you using?

    Ronald Fischer <>
Re: how to copy files with spaces in name
by shmem (Canon) on Mar 06, 2009 at 10:03 UTC
    problem is that file names with spaces cause this line to fail

    Show your failing code. I suspect your directory reading code to cause the problem. Did you check that $groupFile actually holds the file name with embedded spaces? This works for me:

    use File::Copy; use Cwd; my $file = 'foo bar'; my $dest = 'dest dir'; my $cwd = getcwd(); my $src = "$cwd/$file"; open my $fh, '>', $file or die "Can't write '$file': $!\n"; close $fh; mkdir $dest or die "Can't mkdir '$dest': $!\n"; copy ($src, $dest) or die "Can't copy '$src' to '$dest': $!\n";

    Also, don't interpolate single variables. Don't say copy ("$groupFile","$errDir"), say copy ($groupFile, $errDir)

      You were right on the money. I wasn't getting the value in $grpfile I was expecting and I was jumping to conclusions. Thanks. I must say I'm impressed by you monks. The helpfulness and consideration given to beginers like me is fantastic. Thanks again everymonk. My beginning Perl book also says not to interpolate single variables and that other programmers will laugh at you behind your back (direct quote) :). I see that it is unnecessary now but are there any more sinister consequences?

        Thanks for the kudos ;-)
        Interpolation stringifies. Sinister consequences come to bite you if you get the habit and do so with references:

        use strict; my $arrayref = [ qw(a b c) ]; my %hash; $hash{abc} = $arrayref; # ok print $hash{abc}->[2],"\n"; # prints c $hash{abc} = "$arrayref"; # oops, reference converted to a string print $hash{abc}->[2], "\n"; # barfs __END__ c Can't use string ("ARRAY(0x9f43c28)") as an ARRAY ref while "strict re +fs" in use at - line 5.
Re: how to copy files with spaces in name
by Anonymous Monk on Mar 06, 2009 at 10:15 UTC
    What does this do for you (be sure to SETUP)
    #!/usr/bin/perl -- use strict; use warnings; use File::Copy; =head1 SETUP echo >"test1fi le" echo >"test1fil e" echo >"test1fil e" md test1ditch dir test1fi* =cut my( @files ) = ("test1fi le","test1fil e", "test1fil e"); for my $file( @files ){ copy("$file","test1ditch/") or warn sprintf 'Copy(q(%s),q(%s)) failed: $!(%d)(%s) $^E(%d)(%s)', "$file","test1ditch/", int($!),$!,int($^E),$^E; } __END__

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://748795]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2017-01-23 03:42 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (190 votes). Check out past polls.