Discussion:
Can file command supports name wildcard?
(too old to reply)
Hans
2006-05-30 20:06:51 UTC
Permalink
Hi,
I have some problems about file handling in Tcl/Tk.
1. Can file command supports name wildcard?
In one directory, I may have some files like file1_may_1.dat and
file1_june_2.dat, Can I use a similar command as "file exists file1*"
to confirm there are some this kind of files? I tested "file exists
file1*" but it doesn't work.
2. Can I get file list which have similar name prefix and order them by
creating time?
same case, Is there a command to let me to get the list of files and
order them by creating time? like "file1_may_1.dat file1_june_2.dat
file1_june_3.dat".

thanks in advance.


Hans Yin
Aric Bills
2006-05-30 20:25:22 UTC
Permalink
1. the file command doesn't support wildcards, but the glob command
does:

set filelist [glob -nocomplain file1*]

2. you'll have to order the file list yourself, but it can be done.
Use [file stat] to get the creation times, and [lsort] to order your
list. Something along these lines, maybe:

proc orderByCTime {pattern} {

set filelist [glob -nocomplain $pattern]
foreach file $filelist {
file stat $file fileinfo
lappend ctimelist [list $file $fileinfo(ctime)]
}
foreach pair [lsort -index 1 $ctimelist] {
lappend sortedfilelist [lindex $pair 0]
}
return $sortedfilelist
}
Cameron Laird
2006-05-30 20:28:13 UTC
Permalink
Post by Hans
Hi,
I have some problems about file handling in Tcl/Tk.
1. Can file command supports name wildcard?
In one directory, I may have some files like file1_may_1.dat and
file1_june_2.dat, Can I use a similar command as "file exists file1*"
to confirm there are some this kind of files? I tested "file exists
file1*" but it doesn't work.
2. Can I get file list which have similar name prefix and order them by
creating time?
same case, Is there a command to let me to get the list of files and
order them by creating time? like "file1_may_1.dat file1_june_2.dat
file1_june_3.dat".
.
.
.
No. And yes.

Start by experimenting with the Tcl command

glob *

inside an interactive session. Let us know
what questions remain unanswered after that.
Hans
2006-05-30 22:29:37 UTC
Permalink
Hi,

Thank you very much, I tried "glob" command, it works fine and it can
also works as a workaround for my first question.

And thanks again for the proc OrderbyCTime.

No remain questions. :) thanks.

Continue reading on narkive:
Loading...