Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: -f not identifying .txt files

by rucha (Initiate)
on Sep 24, 2010 at 09:56 UTC ( [id://861769]=note: print w/replies, xml ) Need Help??


in reply to Re: -f not identifying .txt files
in thread -f not identifying .txt files

@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 Dumper.pl 09/23/2010 10:49 AM 38,056 Dumper.pm 09/23/2010 11:32 AM 604 ex_47.pl 09/14/2010 11:23 AM 1,591 ex_54FINAL.pl 09/02/2010 01:37 PM 2,081,634 perltut.pdf 09/14/2010 11:10 AM 1,938 prac14.pl 09/14/2010 10:47 AM 160 prac15.pl 09/21/2010 04:11 PM 216 prac16.pl 09/23/2010 10:49 AM 628 prac17.pl 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 Dumper.pl Dumper.pm ex_47.pl ex_54FINAL.pl perltut.pdf prac14.pl prac15.pl prac16.pl prac17.pl q.txt qw.txt qwe.txt qwer.txt qwert.txt List of all files with last modified (no. of days ago) prac17.pl -------------> 1.1802662037037 Dumper.pl -------------> 1.18054398148148 prac16.pl -------------> 2.95675925925926 prac14.pl -------------> 10.1655902777778 prac15.pl -------------> 10.1818171296296 ex_54FINAL.pl -------------> 10.1564699074074 ex_47.pl -------------> 1.15052083333333 Dumper.pm -------------> 1.17990740740741 List of files modified less than 5 days ago prac17.pl -------------> 1.1802662037037 Dumper.pl -------------> 1.18054398148148 prac16.pl -------------> 2.95675925925926 ex_47.pl -------------> 1.15052083333333 Dumper.pm -------------> 1.17990740740741

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

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

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

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

    All dogma is stupid.
      Thank you all !

      I prepended the directory path to the filename, and now it's printing all the files along with last modified no. of days ago correctly.
Re^3: -f not identifying .txt files
by dHarry (Abbot) on Sep 24, 2010 at 11:36 UTC

    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.

    Harry

Re^3: -f not identifying .txt files
by roboticus (Chancellor) on Sep 24, 2010 at 10:41 UTC

    rucha:

    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.

    ...roboticus

      Hi, the file I'm running is in D:/perlnew/ex
      Its path is D:/perlnew/ex/ex_59a.pl

      The program is extracting(?) filenames from the specified directory (which I passed as the argument), not my current directory..

        rucha:

        As I suspected ... then apply tirwhan's patch, and you should be good to go.

        ...roboticus

        Update: Added link and fixed typo (sorry, tirwhan, I misspelled your name the first time!)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://861769]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found