Jump to content

Recommended Posts

Posted

The question looks simple, but I couldn't sort it out quickly, also via search.

How do I get the name of the "youngest" file in a directory?

(Directory may have 1000s of files so if there is solution without looping all files it would be better)

Thank you.

Posted

The question looks simple, but I couldn't sort it out quickly, also via search.

How do I get the name of the "youngest" file in a directory?

(Directory may have 1000s of files so if there is solution without looping all files it would be better)

Thank you.

I wanted to solve it for kicks, so here's what I did:

#include <Array.au3>

Dim $array[1] = [0]
$dir = 'c:\'
$start = FileFindFirstFile($dir & '*.*')

While 1
    $file_found = FileFindNextFile($start)
    If @error Then ExitLoop
    $time = FileGetTime($dir & $file_found, 1, 1)
    _ArrayAdd($array, $time & '|' & $file_found)
WEnd

_ArraySort($array)
$array[0] = UBound($array) - 1  ;yes yes I know, but I like 0 to = total

_ArrayDisplay($array, 'with date/time (YYYYMMDDHHMMSS)')

For $i = 1 To $array[0]
    $file_only = StringSplit($array[$i], '|')
    $array[$i] = $file_only[2]
Next

_ArrayDisplay($array, 'without date/time')

I don't think you could actually do it without a loop? :whistle:

Posted

The question looks simple, but I couldn't sort it out quickly, also via search.

How do I get the name of the "youngest" file in a directory?

(Directory may have 1000s of files so if there is solution without looping all files it would be better)

Thank you.

Run() with "dir /od". Then parse the output. The last line contains the "youngest" file.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Posted

Run() with "dir /od". Then parse the output. The last line contains the "youngest" file.

Cheers

Kurt

Thank xcal ! Very good generic script.

I guess the solution of Kurt is in my situation better.

Posted

dir /o-d and parse the first file name may be quicker. :lmao:

In general you are right, but the time difference would not be very big, something around 0.05 sec... ;)

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...