Generally files with spaces are no problem as long as you are not using the shell. You didn't include your code for the copy function, but if it works on directories without spaces, I expect it uses the cp shell command (which can be dangerous depending on how it is used and exactly how it is implemented).

The code below will be safe as it never passes the file names to the shell.

#!/usr/bin/perl use strict; use warnings; use Path::Class; use File::Copy::Recursive qw/ dircopy /; my $source = "/pr/perl_by_example"; my $dest = "/pr/book"; for my $path (dir($source)->children) { next if $path =~ /\./; my $target = dir($dest, $path->basename); dircopy $path, $target or die "Error copying $path: $!"; }

dir($source)->children will not include the '.' and '..' special directories, but will include everything else (including hidden files and directories). Thus, I left your $path =~ /\./ test in place. However, that test looks suspicious since it will match a '.' anywhere in the name (beginning, middle, end) so will usually match file names as well (but not always, since not all file names have an extension). To exclude hidden files and directories use: next if $path =~ /^\./; To exclude all files and only copy directories next unless $path->is_dir; If you want to exclude both (all hidden files and directories as well as all file), I would suggest including both tests separately:

for my $path (dir($source)->children) { next if $path =~ /^\./; # exclude hidden files and directories next unless $path->is_dir; # exclude files my $target = dir($dest, $path->basename); dircopy $path, $target or die "Error copying $path: $!"; }

Good Day,
    Dean


In reply to Re: Copy dir with space by duelafn
in thread Copy dir with space by adriang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.