Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

-f not identifying .txt files

by rucha (Initiate)
on Sep 24, 2010 at 08:32 UTC ( #861751=perlquestion: print w/replies, xml ) Need Help??
rucha has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to Perl. I have a problem: I am using readdir to store names of all files present in a directory, to an array. When I use -f function on each element of array using foreach, it does not pick up .txt files, but it identifies .pl files.

Replies are listed 'Best First'.
Re: -f not identifying .txt files
by tirwhan (Abbot) on Sep 24, 2010 at 08:41 UTC

    Maybe your .txt files are actually soft links? Maybe you're not correctly specifying the path when testing with -f? Or maybe your problem is on line 42, which specifies the flying pink unicorns?

    Seriously, read through your question again, how are we supposed to know what you're doing wrong if you're not showing it? We can't help you with code we don't see. Show us the code in question (only the code for this specific question, no need to post your entire script), and possibly show the output of ls -al of the directory you're examining, and you'll get a more helpful answer.

    All dogma is stupid.
Re: -f not identifying .txt files
by dHarry (Abbot) on Sep 24, 2010 at 08:39 UTC

    This makes no sense, show some code please.

      @dHarry, @tirwhan: Sorry for my mistake. I joined the forum today and was expecting that there will be an option to upload my code file directly. I couldn't find it. Discovered the tags later. Anyway, here's my code. (I have given the whole code so that exact mistake can be pointed out)

      $, = "\t\t"; sub del { my $p = shift; my $days = shift; print $p; opendir ($dh, $p) || die "Some error: $!"; @flist = readdir($dh); closedir $dh; print "\nThe list of files is: \n"; foreach(@flist) { print "\n$_"; } my %list = (); foreach(@flist) { if(-f $_) { $list{$_}; $list{$_} = -M $_; } } print "\n\nList of all files with last modified (no. of days ago)\ +n"; foreach $si (keys %list) { print $si ,"------------->",$list{$si}; print "\n"; } print "\n\n\n"; print "\nList of files modified less than $days days ago\n"; foreach $si (keys %list) { if($list{$si} < $days) { print $si ,"------------->",$list{$si}; print "\n"; } } } &del ("D:/perlnew/ex/comp", 5); #*********************************************************** #Directory listing through command line: Directory of D:\perlnew\ex\comp 09/23/2010 02:48 PM <DIR> . 09/23/2010 02:48 PM <DIR> .. 08/30/2010 05:56 PM 377 a.txt 09/24/2010 11:56 AM 28 as.txt 09/24/2010 11:55 AM 20 blah.txt 09/23/2010 10:48 AM 38,056 09/23/2010 10:49 AM 38,056 09/23/2010 11:32 AM 604 09/14/2010 11:23 AM 1,591 09/02/2010 01:37 PM 2,081,634 perltut.pdf 09/14/2010 11:10 AM 1,938 09/14/2010 10:47 AM 160 09/21/2010 04:11 PM 216 09/23/2010 10:49 AM 628 09/24/2010 11:55 AM 20 q.txt 09/24/2010 11:55 AM 23 qw.txt 09/24/2010 11:55 AM 16 qwe.txt 09/24/2010 11:55 AM 25 qwer.txt 09/24/2010 11:55 AM 18 qwert.txt 17 File(s) 2,163,410 bytes 2 Dir(s) 52,993,323,008 bytes free #************************************************************** #OUTPUT OF PROGRAM: D:/perlnew/ex/comp The list of files is: . .. a.txt as.txt blah.txt perltut.pdf q.txt qw.txt qwe.txt qwer.txt qwert.txt List of all files with last modified (no. of days ago) -------------> 1.1802662037037 -------------> 1.18054398148148 -------------> 2.95675925925926 -------------> 10.1655902777778 -------------> 10.1818171296296 -------------> 10.1564699074074 -------------> 1.15052083333333 -------------> 1.17990740740741 List of files modified less than 5 days ago -------------> 1.1802662037037 -------------> 1.18054398148148 -------------> 2.95675925925926 -------------> 1.15052083333333 -------------> 1.17990740740741

      When I use (!-d $_) to identify files, .txt, .pdf files are printed, but their last modified (no. if days) is not printed.

        Check the working directory in which you are executing the script. I'd wager you have the .pl and files in there, but not the others. To fix your script change, on line 20 of the code given

                if(-f $_)
                if(-f "$p/$_")

        All dogma is stupid.

        I think the solution has already been suggested, I would just like to point you to the documentation (readdir):

        If you're planning to filetest the return values out of a readdir, you'd better prepend the directory in question. Otherwise, because we didn't chdir there, it would have been testing the wrong file.



        If I had to guess, I'd guess that you have the .pl and .pm files in your current directory, and that d:/perlnew/ex/comp is a test directory, which contains the .pl and .pm files, along with other files. Since there's no directory prefix, the -f function is checking your local directory rather than the directory you've done the readdir from.

        If your current directory is actually d:/perlnew/ex/comp when you run your script, then I can't think of a reason that readdir would see the files and -f wouldn't.


Re: -f not identifying .txt files
by betterworld (Curate) on Sep 24, 2010 at 16:04 UTC

    As others have said, the problem is probably that you don't have the full path name in your -f argument.

    I like to use Path::Class because it avoids these problems:

    use Path::Class qw(dir); my $dir = dir("/path/to/dir"); my @files = $dir->children; my $examplefile = $files[0] or die "no files"; print "is plain file" if -f $examplefile; # Or: print "is plain file" unless $examplefile->is_dir;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://861751]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-03-30 13:03 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (357 votes). Check out past polls.