in reply to Copy dir with space

Hi adriang,

I contend there are several issues here, least of which is the filename containing a space.

Firstly, use strict and warnings, if you did already, pls include with code example to show this. Secondly, copy, afaik is not a perl built-in (perldoc -f copy = not found).

copy is a utility function which can be found in another module similar to File::Spec, I cannot recall which this moment. So basically, opendir to copy files from, readdir, filter, read contents into filehandle, open new filehandle name in destination directory, print contents.

Untested! But, principally something like this, if the 'copy' containing module cannot be used.

use strict; use warnings; use File::Spec::Functions qw/catfile/; my $dirnameto = catfile(qq{pr book}); my $dirnamefrom = catfile(qq{pr perl_by_example}); opendir( $dhandlefrom, $dirnamefrom )or die; while (my $file=readdir($dirhandlefrom)) { next if $file =~ /\./; my $filecontents; {local $/ = undef; # slurp mode; open( my $tfh, '<', catfile( qq{$dirnamefrom $file} ) ) or die; $filecontents = <$tfh>; close $tfh; # for clarity; } open( my $filecopy, '>', catfile( qq{$dirnameto $file} )) or die; + print { $filecopy } $filecontents; close $filecopy; }

DC

Replies are listed 'Best First'.
Re^2: Copy dir with space
by adriang (Sexton) on Jul 10, 2014 at 10:40 UTC

    Thanks for the explain and for the answer. I'm afraid it does not work, I get: " Died at /proj/work.pl line 9. " Something is not working with "catfile"

      not quite. Couple of straight forward syntax errors. Catfile is probably fine, as the program died at an expected line, and, has compiled without complaining about the existence of the function catfile. But the arguments passed to it are amiss. This is probably due to the paths not being stated relatively in the rough draught.

      There was a thirdly, I did not mention previously. Thirdly, Paths and Files are different entities. You are using two directory opens when that is not needed for this copy operation. I think you may need to review the difference between a path and a file. To further complicate this issue, a Directory is a File. Well it is easier to say that both a directory and a file can be recognised as a string (or path). You then need to treat that string correctly depending whether it is a file, or a directory.

      P.S. That is what untested means, 'probably does not work, but you get the gist...'

      hope this helps. DoC