How to Get the Latest File in a Folder

Get most recent file in a directory on Linux

ls -Art | tail -n 1

Not very elegant, but it works.

Used flags:

-A list all files except . and ..

-r reverse order while sorting

-t sort by time, newest first

Get second newest file in a folder

Looks like you actually want getmtime, not getctime (since you're showing us a screenshot showing modification times).

python: How to get latest file in a directory with certain pattern

You could simply go like this:

def newest(DIR_PATH):
files = os.listdir(DIR_PATH)
FILE_LIST = [os.path.join(DIR_PATH, BASENAME) for BASENAME in files if not BASENAME.endswith("trimmed.json")]
return max(FILE_LIST, key=os.path.getctime)

Powershell: Get-ChildItem, get latest file of files with similar names

Ok, now that you've provided the whole list that you filter against I can write up a real answer. Here we'll group by file name, then sort each group and grab the last one from each group:

Get-ChildItem -Path c:\tm1 | 
Where-Object { $_.Name -match '^RR_Prognos.*|^AllokeringBogNycklar.*|^Aktivversion.*|^AllokeringNycklar.*|^HR_prognos.*|^KostnaderDK.*|KostnaderProdukt_prognos.*|^Parametrar_prognos.*|ProduktNyckel_prognos apr.*' } |
Group {$_.Name -replace '.*?(^RR_Prognos|^AllokeringBogNycklar|^Aktivversion|^AllokeringNycklar|^HR_prognos|^KostnaderDK|KostnaderProdukt_prognos|^Parametrar_prognos|ProduktNyckel_prognos apr).*','$1'} |
ForEach-Object {
$_.Group |
Sort-Object -Property LastWriteTime -Descending |
Select -First 1
} |
Select-Object LastWriteTime,FullName


Related Topics



Leave a reply



Submit