12

I'm looking for a way in Windows to list all files in a given directory and its subdirectories, sorted so that the newest files are on top. Is there any easy way to do this? I'd also settle for a Perl script.

6 Answers 6

3

If you're still interested in a scripted solution, I've quickly thrown something together which will display files from all directories together:

#!C:/Perl/bin/perl.exe
use File::Find;

my %files = ();

sub process
{
    $files{$_} = (stat($_))[9] unless -d $_;
}

find (\&process, $ARGV[0]);

foreach my $key(sort {$files{$b} <=> $files{$a}} keys %files)
{
    print "$key\n";
}

Files are displayed newest to oldest, showing only the file name. This way you can easily pipe the output of this script to another tool for processing without having to worry about stripping excess output.

Usage: perl find.pl <starting_directory>

4
  • thanks. i guess this should have been a stackoverflow question if it went in this direction. :)
    – Kip
    Feb 15, 2010 at 22:54
  • 1
    a few changes i made (for the help of anyone who'd happen to come across this): my $dir = ($#ARGV >= 0 ? $ARGV[0] : '.'); so that it will scan current dir if directory parameter isn't given, and to include the time that is printed: print strftime('%Y-%m-%d %H:%M:%S', localtime((stat($key))[9])) . " $key\n";
    – Kip
    Feb 15, 2010 at 22:55
  • 1
    @Kip, the mtime should be stored in $files{$key} if you want to use that for strftime when printing to avoid a second call to stat(). If you plan on piping it to another program, I've also modified it to display the full path of the filenames. You can grab that here: pastebin.ca/1797748
    – user1931
    Feb 15, 2010 at 22:57
  • thanks, you're faster than i am. the stat command only worked for files in the current directory too, since you were only storing the last part of the filename.
    – Kip
    Feb 15, 2010 at 23:05
7

Like this: dir /s /o:-d

1
  • 1
    hmm... not quite ideal, this still lists each directory individually, but i can work with it
    – Kip
    Feb 15, 2010 at 20:55
5

If you have cygwin or similar installed then find . -type f | xargs ls -tr will do the trick. You can almost certainly do similar with Microsoft's powershell.

4

In case you're using Everything, just type the path and sort by Date Modified and you will get all files in this folder in that order, regardless the subdirectory.

Everything is freeware, a portable version is available.

2

If you have something like Git Bash or Cygwin installed, you should be able to use one of the answers from over here: https://stackoverflow.com/questions/4561895/how-to-recursively-find-the-latest-modified-file-in-a-directory

One that worked well for me is: find . -type f -printf '%TF %TT %p\n' | sort -r

You can also add | head -10 to the end of that to see just the 10 most recent, say.

1

If you don't mind coding (though this takes the question into Stack Overflow territory) then there's the Directory and File classes in C#.

The GetFiles method has an overload that will return all filenames in all subdirectories. You can then loop over this list, calling GetLastWriteTime to get the modified date/time. Store the name and time in a dictionary, sort on the time and print out the filenames.

There's also the DirectoryInfo and FileInfo classes.

I should have added that all of this functionality is available via Powershell, so you don't have access to a full IDE.

2
  • thanks. i am a developer though mainly in java. i was asking here hoping a shell option was available
    – Kip
    Feb 15, 2010 at 22:45
  • @Kip - I realised last night after I logged out that I should have mentioned Powershell - technet.microsoft.com/en-us/scriptcenter/dd742419.aspx - it's a scripting language but you have access to the .NET runtime.
    – ChrisF
    Feb 16, 2010 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .