Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

File list by date

by rasmus (Initiate)
on Dec 21, 2000 at 18:19 UTC ( [id://47783]=perlquestion: print w/replies, xml ) Need Help??

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

I am retrieving a list of files in a directory by using the following code:

opendir(root, "x:\\"); while ($files = readdir(root)) { if ($files >= 1) { print "$files\n"; } } closedir(root); exit;
Is it possible to get the resulting list sorted by the file's timestamp?

Replies are listed 'Best First'.
Re: File list by date
by kilinrax (Deacon) on Dec 21, 2000 at 18:29 UTC
    Use 'sort' with a file test operator ('-M' for modification time, '-A' for access time, et.c.).
    The following script this works fine on my linux box; apologies if it doesn't port to Windows ;-}
    #!/usr/bin/perl -w use strict; chdir '/winD' or die "Could not change directory: $!"; opendir DIR, '.' or die "Could not open directory: $!"; my @files = readdir DIR; closedir DIR; my @sortedfiles = sort { -M $a <=> -M $b } @files; $, = "\n"; print @sortedfiles;
Re: File list by date
by mirod (Canon) on Dec 21, 2000 at 18:45 UTC

    Time for the Schwartzian transform!

    #!/bin/perl -w use strict; opendir(ROOT, ".") or die "Arghh!!! $!"; my @sorted_files= map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ -M $_, $_] } readdir( ROOT); print join "\n", @sorted_files; exit;
Re: File list by date
by davorg (Chancellor) on Dec 21, 2000 at 18:32 UTC

    Something like this perhaps...

    opendir(ROOT, $dir) || die "$!\n"; foreach (sort { -M $b <=> -M $a } readdir(ROOT) ) { print "$_\n"; }

    Update: snax below quite rightly points out a bug in this code.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Careful! Checking the description of readdir it says one must either chdir or prepend the $dir if you want to do filetests.
      foreach (sort {-M qq($dir/$b) <=> -M qq($dir/$a)} readdir(ROOT)) { print $_, $/; }
      for example.

        Using this method (snax), is there a way to only list files that are one hour old, or two hours old, etc.?
Re: File list by date
by Fastolfe (Vicar) on Dec 21, 2000 at 19:24 UTC
    What is the ($files >= 1) bit for? You're putting a string (the filename) into a numeric context here? Are your filenames numeric or are you trying to test to see if $files is non-empty? If it's the latter, consider just doing if ($files) { ... }, but realize that your while statement above that would have done that test itself, or else you wouldn't be inside the loop! The plural itself is kind of a misnomer as well, but it's not hurting anything.

    In addition, expanding upon your 'while' test, if you have a filename of 0 (zero), your loop will exit, even if there are more files to be read. Consider checking for defined-ness: while (defined ($file = readdir(DIR))) { ... }

Re: File list by date
by I0 (Priest) on Dec 21, 2000 at 18:55 UTC
    #or for $files( map{ unpack'x4a*',$_ } sort map{pack 'Na*',(stat "x:/$_")[9],$_} readdir(root) ){ print "$files\n"; }
Re: File list by date
by jeroenes (Priest) on Dec 21, 2000 at 20:21 UTC
    Just for fun, let's try to do this with Supersplit, which can be found here. (It's not yet in CPAN ;-)).
    use SuperSplit; if ( $^O =~ /linux/){ $dir = `ls -l`; $col = 4; }else{ #sorry, no macs ;-) $dir = `dir -w`; $col = 2; } @$sortedref = sort{ $a->[$col] <=> $b->[$col] } @{supersplit('\s+','\n',$dir)}; print superjoin( $sortedref );
    I tested it and it works under linux. Would be neater without the OS-checking, though.

    Have fun with it

    Jeroen
    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

      Heh -- non Perl advice follows :)

      If you're using linux (or most any *nix -- I think this flag is the same for most versions of ls) you should just use the -t flag and let ls do the time sorting for you. Or use -T for most recent first.

      Similarly, there are flags for dir that allow it to do the sorting, too. See dir /? for clues.

        Thanks for this thread. It's got me part of the way through a dilemma I'm trying to figure out. Can anyone tell me how I might add something like --

        "if any of these files is/are older than 5 minutes, create a file named warning_x in folder_x"?

        Thanks for any input. Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-19 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found