Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

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>

share|improve this answer
    
thanks. i guess this should have been a stackoverflow question if it went in this direction. :) –  Kip Feb 15 '10 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 '10 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 –  John T Feb 15 '10 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 '10 at 23:05
add comment

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.

share|improve this answer
    
thanks. i am a developer though mainly in java. i was asking here hoping a shell option was available –  Kip Feb 15 '10 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 '10 at 8:31
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Like this: dir /s /o:-d

share|improve this answer
    
hmm... not quite ideal, this still lists each directory individually, but i can work with it –  Kip Feb 15 '10 at 20:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.