in reply to Re^5: dir handle
in thread dir handle

system : windows 7

i search for file::tiny in ppm but not found

my logic:

open both dir and take list of filename in array so one contain 2 and other 3 then in for loop i run index of array to capture individual file and apply copy() from File::Copy but when i m using $test1$x one dir contain one less file so i got an err so my copy statement not executed.. please help....
use strict; use warnings; use File::Copy; my @test1; my @test2; print "Enter the file path form where you want to copy file:\n"; chomp(my $path1 = <STDIN>); opendir DH, "$path1" || die "$!"; @test1 = readdir (DH); print "@test1\n"; print "Enter the file path form where you want to copy file:\n"; chomp(my $path2 = <STDIN>); opendir DH, "$path2" || die "$!"; @test2 = readdir (DH); print "@test2\n"; my $count = @test1; my $count2 = @test2; my $x = 0; for ($x = 0; $x<$count2; $x++) { if($test1[$x] ne $test2[$x]) { print "$test1[$x] : $test2[$x]\n"; # copy("$test1[$x]", "$path2") or warn "cannot copy $test1[$x]" +; # copy("$test2[$x]", "$path1")or warn "cannot copy $test2[$x]"; +; } }

Replies are listed 'Best First'.
Re^7: dir handle
by RichardK (Parson) on May 27, 2014 at 12:25 UTC

    If you just want a list of the files in a directory then it's easier to just use glob

    Also note that the files in a directory are not ordered, so any list will be in a random order unless you sort it.

    Your code could then look something like this :-

    use File::Basename; my @files = glob('/path/to/files/*'); foreach my $file (@files) { my $name = basename($file); if (! -x "/new_path/$name") { # copy file here } }

    Have a look at the help for File::Basename or better yet File::Spec for functions to safely handle paths & filenames

Re^7: dir handle
by ww (Archbishop) on May 28, 2014 at 01:57 UTC

    "i search for file::tiny in ppm but not found"

    That's not exactly surprising. In the prior discussion Anonymonk and choroba both mentioned Path::Tiny; you said you were trying to use File::Copy. PPM doesn't know about "file::tiny" because -- if it exists -- it's not on CPAN, or any other standard source.


    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.

    check Ln42!