Home » Questions » Computers [ Ask a new question ]

How to find newest files in a directory, including subdirectories?

How to find newest files in a directory, including subdirectories?

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.

Asked by: Guest | Views: 52
Total answers/comments: 3
Guest [Entry]

"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>"
Guest [Entry]

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.
Guest [Entry]

"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."
"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."