0

i have a folder where there are subfolder with dates example : Parent/2020-11-09_15.47.36 Parent/2020-11-10_16.52.02 and so on. now I want to find out which is the latest date folder inside Parent folder using tcl command.

output should be : 2020-11-10_16.52.02

3
  • 1
    Do you want to inspect the date this folder was created, or may the result be based on the folder's name only. – Gerard H. Pille Nov 11 '20 at 10:15
  • i want the latest date folder created inside Parent folder. – Panda Nov 11 '20 at 10:53
  • find /Parent/20* -type d -prune -exec ls -d {} \; | tail -1 i have used this in shell to get the latest folder, but want the same in tcl – Panda Nov 11 '20 at 10:55
1

You picked a sensible datetime format that sorts the same lexically as chronologically.

In Tcl you would do

cd Parent
set subdirs [glob -type d *]
set last [lindex [lsort $subdirs] end]

If you have other subdirectories in there that would interfere with the sorting, and you want to filter them out, you can do:

set subdirs [lmap dir [glob -type d *] {
  if {![regexp {^\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2}$} $dir]} then continue
  set dir
}]
1
lindex [ lsort -decreasing [ glob -type d Parent/* ] ] 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.