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

Hello Monks

I am using the following code to check whether the files in directory
ending with .Z or .tar or .zip and take only those files into
array. Though there are files ending with .Z or .tar
when I run the script it says there are no files...
I am unable to figure out the error in if condition
use strict; use File::Path; use File::Find; my $dir="/home/testing"; my @dir = ($dir); my @srcfiles; find(\&srcfind, @dir); find(\&srcfind, @dir); sub srcfind { my $file = $File::Find::name; if ( -f $file && $file =~ /\.Z$/ || $file =~ /\.zip$/ || $file =~ / +\.tar$/ ){ push (@srcfiles, $file); } } if(@srcfiles){ print "There are some files\n"; }else{ print "There are no files \n"; }
Thanks & Regards
Sridhar

Replies are listed 'Best First'.
Re: FindFiles error in condition
by liverpole (Monsignor) on Dec 26, 2006 at 15:59 UTC
    Hi mantra2006,

    It's because you're constructing the full pathname of the file with my $file = $File::Find::name;, but that file doesn't exist within the current directory being searched.

    If you read the documentation for File::Find, you'll see that you probably want $_ (the current filename only) instead of $File::Find::name (the full pathname).  And if you print out what directory you're in (below with getcwd()), it will make things even more clear:

    use strict; use warnings; use File::Path; use File::Find; use Cwd; my $dir="/home/testing"; $dir = "./a"; my @dir = ($dir); my @srcfiles; find(\&srcfind, @dir); sub srcfind { ## my $file = $File::Find::name; -- wrong my $file = $_; printf "TFD> in dir %s, file $file\n", getcwd(); if (-f $file) { if ($file =~ /\.Z$/ or $file =~ /\.zip$/ or $file =~ /\.tar$/ +) { push @srcfiles, $file; } } } if(@srcfiles){ print "There are some files\n"; }else{ print "There are no files \n"; } __END__ Prints: TFD> in dir C:/Documents and Settings/liverpole/targetdir, file . TFD> in dir C:/Documents and Settings/liverpole/targetdir, file a TFD> in dir C:/Documents and Settings/liverpole/targetdir, file a.Z TFD> in dir C:/Documents and Settings/liverpole/targetdir, file a.zip TFD> in dir C:/Documents and Settings/liverpole/targetdir, file b.Z TFD> in dir C:/Documents and Settings/liverpole/targetdir, file b.zip TFD> in dir C:/Documents and Settings/liverpole/targetdir, file c.Z There are some files

    As you can see, I also separated out the conditionals (existence of the file, and regex match) to make things clearer, and easier to debug.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: FindFiles error in condition
by johngg (Canon) on Dec 26, 2006 at 16:16 UTC
    liverpole and andyford have pointed you to probable issues with working directory. I just want to suggest that your if condition in sub srcfind might be simpler to understand and save some typing if you move the alternation into the regular expression and do just one match, like this

    if ( -f $file && $file =~ m{\.(?:Z|zip|tar)$} )

    I hope this is of use.

    Cheers,

    JohnGG

Re: FindFiles error in condition
by andyford (Curate) on Dec 26, 2006 at 16:04 UTC

    Your code is working great for me once I changed the directory to my home dir.
    Perhaps "/home/testing" is not where you really want to look?

    Two suggestions:

    • "use warnings"
    • "use warnings"

      non-Perl: Andy Ford

Re: FindFiles error in condition
by mantra2006 (Hermit) on Dec 26, 2006 at 16:26 UTC
    Hey liverpole,andyford,johngg,

    Thank you for the replies...I understood the problem...after the changes script worked..

    Thanks & Regards
    Sridhar