从目录中查找特定的扩展名文件,并根据TCL中的修改日期打印最后5个扩展名文件
我有一个目录,其中有一些特定扩展名的文件以及其他从目录中查找特定的扩展名文件,并根据TCL中的修改日期打印最后5个扩展名文件,tcl,Tcl,我有一个目录,其中有一些特定扩展名的文件以及其他 档案 我需要的最后五个文件,根据其修改日期的特定扩展名 如果超过5个,则只打印最后5个,如果少于5个,则打印 那种,全部打印出来 你能帮我写这个tcl代码吗 例1: 由于本例中少于5个.abc文件,因此我们需要按与上次修改日期相反的顺序收集所有文件: 目录:TESTCASE 档案: - apple_12.abc_no - banana.abc - dog.xyz - place.txt - sofa_1_2_12.abc - hello.org
档案
那种,全部打印出来
.abc
文件,因此我们需要按与上次修改日期相反的顺序收集所有文件:
目录:TESTCASE
档案:
- apple_12.abc_no
- banana.abc
- dog.xyz
- place.txt
- sofa_1_2_12.abc
- hello.org
输出:
- sofa_1_2_12.abc
- banana.abc
- apple_12.abc_no
例2:
由于本例中有5个以上的.abc文件,我们需要按与上次修改日期相反的顺序持续5个:
档案:
- apple_12.abc_no
- banana.abc
- dog.xyz
- place.txt
- sofa_1_2_12.abc
- hello.org
- world.abc
- stack_133_gre.abc
- potato.txt
- onsite_all.abc
- list.abc
输出:
- list.abc
- onsite_all.abc
- stack_133_gre.abc
- world.abc
- sofa_1_2_12.abc
我试图通过glob命令从目录TESTCASE
中查找.abc
文件:
set PWD $pwd
set files [glob -tails -directories $PWD/$TESTCASE/*.abc*]
puts $files
但如何跟踪最后五个或更少,是我被卡住的地方。我们尝试在unix中使用
tail-f filename
。tcl中有什么方法可以做到这一点吗?您当前的代码存在一些问题。试试这个:
# Proc to get latest 5 modified files
proc get_latest {pwd files} {
# Container for these files
set newList [list]
# Loop through each files and get the modified date
foreach f $files {
lappend newList [list $f [file mtime $pwd/TESTCASE/$f]]
}
# Sort the list on date, putting latest first
set newList [lsort -decreasing -index 1 $newList]
# Return top 5
return [lrange $newList 0 5]
}
# Get path of script
set PWD [pwd]
# Get files with extension
set files_with_ext [glob -tails -directory $PWD/TESTCASE *.abc*]
# Get top 5 files
set top_five [get_latest $PWD $files_with_ext]
# Finally print the file names, removing the introduced timestamps.
foreach f $top_five {
puts [lindex $f 0]
}
你指的是最后一次使用,你指的是最近修改的日期?是的,jerry,last used意思是最后一次修改。I jerry,请帮助。你试过脚本了吗?脚本正在运行。非常感谢Jerry:)为了速度和正确性,我将您的代码改为使用
list
;它们以操作系统的基本文件列表API认为最好的顺序返回。(这可能是磁盘上的顺序。就像所有人都关心的那样。)