My first thought is to do
ls -l | head
That would work perfect, if we were running on a *nix platform. Sometimes, it's hard to be a programmer in a windows environment.
My next thought was to use ColdFusion; something along the lines of
<cfdirectory
action="list"
name="dirContents"
directory="#GetDirectoryFromPath(GetCurrentTemplatePath())#"
sort="DateLastModified DESC">
<cfloop query="dirContents" endrow="10">
<cfoutput>#dirContents.Name# #dirContents.DateLastModified#
</cfoutput>
</cfloop>
Unsurprisingly, this took way too much time, as ColdFusion was fetching meta data for each and every file in the directory. I would have liked to try using an event gateway, but alas, this is Standard Edition ColdFusion instead of Enterprise. I also looked into Java, but found similar limitations. It was quite obvious that the only way to do this in a reasonable time was to interact with the file system at the OS level.
I'll spare everyone the gruesome details of the Google searches, but here is the batch file that I came up with.
@echo off & setLocal enabledelayedexpansion
set dirpath=.
set limit=10
set order=/O:-D
if not [%1]==[] set dirpath=%1
if not [%2]==[] set limit=%2
if not [%3]==[] set order=%3
SET /a N=0
for /F "skip=5 tokens=* delims= " %%i IN ('dir %dirpath% %order%') do (
SET /a N+=1
echo %%i
IF !N! GEQ %limit% GOTO end
)
:end
The trickiest thing was getting the counter to increment inside the loop iterations. Sounds easy, but in a batch file it is not intuitive at all, and I had trouble finding good examples. It came down to a combination of the setLocal enabledelayedexpansion at the top and the syntax using the exclamation marks, !N! instead of percent signs.