ruqui has asked for the wisdom of the Perl Monks concerning the following question:

I need to move using Perl the contents of a Windows directory containing parenthesis in its name to another folder, the problem is that it always fails because of the parenthesis. I've tried using either glob or opendir + readdir functions but I always get errors, for example:
#!/usr/bin/perl use File::Copy; $src = "c:\\temp (1)J"; for $f (glob("$src\*")) { move($f, "\out"); }
The above fails as well as this one:
#!/usr/bin/perl use File::Copy; $src = "c:\temp (1)"; opendir DIR, $src; while ($f = readdir(DIR)) { move("$src\$f", "\out"); }
Any ideas of how to solve this?

Replies are listed 'Best First'.
Re: using File::Copy move() in Windows directories containing parentheses
by Athanasius (Bishop) on Feb 04, 2017 at 04:12 UTC

    Hello ruqui,

    The glob problem can be solved by adding another level of quotes:

    for my $f (glob(qq["$src\\*"])) {

    as explained in the documentation:

    Note that glob splits its arguments on whitespace and treats each segment as separate pattern. ... If you want to glob filenames that might contain whitespace, you'll have to use extra quotes around the spacey filename to protect it.

    Note also that inside double quotes, a single backslash combines with the immediately-following character to create an escape sequence. So if you want a literal backslash, you must either double the backslash ("\\out") or use single quotes ('\out').

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: using File::Copy move() in Windows directories containing parentheses (Path::Tiny)
by beech (Parson) on Feb 04, 2017 at 07:52 UTC

    Hi,

    glob likes forward slashes too

    OTOH Path::Tiny uses File::Copy, its convenient

    #!/usr/bin/perl -- use Path::Tiny; my $src = 'C:/temp (1)J'; my $dst = 'C:/out/'; for my $file ( path( $src )->children ){ if( $file->copy( $dst ) ){ $file->remove; } } __END__ $ tree -f -a "temp (1)J" out temp (1)J |-- temp (1)J/1 `-- temp (1)J/2 out 0 directories, 2 files $ perl mytask.pl $ tree -f -a "temp (1)J" out temp (1)J out |-- out/1 `-- out/2 0 directories, 2 files
      using Path::Tiny really helped a lot (with globs I had problems also when the destination folder contains spaces, with Path::Tiny all problems are gone) Thanks!