在 Tcl 中获取按年龄(mtime)排序的文件列表的命令 [英] command to get filelist ordered by age (mtime) in Tcl
问题描述
我正在尝试使用 Tcl 简单地处理目录中的文件列表,但想按年龄顺序处理它们(从最旧的 mtime 到最新的).我希望 glob 或 lsort 中的某种参数按文件 mtime 排序,但我没有看到这样的选项.
I'm trying to simply process a list of files in a directory using Tcl, but want to process them in age order (oldest mtime to newest). I expected some sort of argument in glob or lsort to sort by file mtime, but I don't see such option.
我试图避免创建自定义函数来执行此操作
I am trying to avoid creating a custom function to do this
是否有我缺少的选项可以内置此功能?
Is there an option which I am missing that will do this built-in?
推荐答案
我不知道,但您当然可以使用适当的选项exec
系统的文件列表命令.
None that I know of, but you could of course exec
your system's file listing command with the appropriate options.
使用 mtime 是一项相当昂贵的操作,因此使用它的应用程序通常会采取快捷方式来避免查询它.使其可移植也会增加开销.
Taking the mtime is a fairly expensive operation, so applications that use it typically take shortcuts to avoid querying for it. Making it portable also adds overhead.
无论如何,它很容易实现:
Anyway, it's easy to implement it:
set files [glob x*]
set fileAndMTime [lmap name $files {list $name [file mtime $name]}]
lmap item [lsort -integer -index 1 $fileAndMTime] {lindex $item 0}
最后一行为您提供了一个文件名列表,按最小 mtime 到最大 mtime 的顺序排序(使用 -decreasing
反转顺序,并注意排序是稳定的).
The last line gives you a list of filenames, sorted in order of least mtime to greatest mtime (use -decreasing
to reverse order, and note that the sort is stable).
文档:文件,glob,lindex,lmap(用于 Tcl 8.5),lmap,lsort
这篇关于在 Tcl 中获取按年龄(mtime)排序的文件列表的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!