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
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>
-
thanks. i guess this should have been a stackoverflow question if it went in this direction. :)– KipFeb 15, 2010 at 22:54
-
1a 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";
– KipFeb 15, 2010 at 22:55 -
1@Kip, the mtime should be stored in
$files{$key}
if you want to use that forstrftime
when printing to avoid a second call tostat()
. 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– user1931Feb 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.– KipFeb 15, 2010 at 23:05
Like this: dir /s /o:-d
-
1hmm... not quite ideal, this still lists each directory individually, but i can work with it– KipFeb 15, 2010 at 20:55
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.
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.
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.
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.
-
thanks. i am a developer though mainly in java. i was asking here hoping a shell option was available– KipFeb 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.– ChrisFFeb 16, 2010 at 8:31