Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Listing files

by oaklander (Acolyte)
on Feb 12, 2002 at 15:26 UTC ( [id://144873]=perlquestion: print w/replies, xml ) Need Help??

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

Please advise how I can list files with with January 2002 (on Windows NT) dates. Here is my attempt at it:
use File::Copy; $dir = "/perl/bin"; sub month { foreach (glob "$_[0]/*") { -d && recursedirs($_) unless -l; if (-f ) { if ((-M) > 01/01/02 && (-M) < 01/31/02 ) { my $file = $_; print "$_\n"; } } } } month("$dir")

Replies are listed 'Best First'.
Getting File Modification Time - Re: Listing files
by metadoktor (Hermit) on Feb 12, 2002 at 17:22 UTC
    This short snippet will return the month and year of whatever file.

    #!/usr/local/bin/perl -w use strict; my ($month,$year)=get_month_year("somefile.txt"); print "$month and $year\n"; sub get_month_year { my $file=shift; my @params=stat $file; my @dparams=localtime($params[9]); my $month=1+$dparams[4]; my $year=1900+$dparams[5]; return ($month,$year); }

    metadoktor

    "The doktor is in."

Re: Listing files
by BeernuT (Pilgrim) on Feb 12, 2002 at 16:31 UTC
    Let me just give you a quick answer to help you on your way. your use of
    ((-M) > 01/01/02 && (-M) < 01/31/02 ))
    is wrong in 2 ways. If I understand your question correctly then -M is not what you want. -M returns the Age of file in days when script started. A small example might be..
    $var = -M "filename.txt";

    also your comparing a number to a string with the use of > and <.
    You probably want to look at perldoc -f stat to work on your problem. If your still having trouble with stat msg me and i'll give you a few hints

    -bn
      I looked at perldoc -f stat and still cant seem to get this to work. Please advise if possible.
Re: Listing files
by BeernuT (Pilgrim) on Feb 12, 2002 at 21:05 UTC
    k, borrowing metadoktor's get_month_year sub, here is what i come up with.
    use strict; use warnings; use File::Find; my $dir = 'c:'; my $search_month = 1; my $search_year = 2002; find(\&wanted, $dir); sub wanted { my $content = $_; if(-f $content) { my($month, $year) = get_month_year($content); if(($month eq $search_month) && ($year eq $search_year)) { print "$content was last accessed in $month of $year\n"; } } } sub get_month_year { my $file = shift; my @params = stat $file; my @dparams = localtime($params[9]); my $month = $dparams[4] + 1; my $year = $dparams[5] + 1900; return($month, $year); }


    this can probably be made smaller but this will be good to help you understand. Play around with the stat command, its easy and you'll thank yourself later.


    -bn
      Can someone explain what this 'shift' word is doing in this subroutine???
      sub get_month_year { my $file = shift; #PLEASE EXPLAIN WHAT SHIFT IS DOING? my @params = stat $file; my @dparams = localtime($params[9]); my $month = $dparams[4] + 1; my $year = $dparams[5] + 1900; return($month, $year); }
        The shift in the code is there to return the directory that is passed in the get_month_year. In the get_month_year sub the parent directory is needed - the shift operator does this by shifting the first value of the array off and returning it (the parent directory 'c:' in this case).

        You can find more info on the shift node.

      Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-23 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found