I want to set the first file in my directory to a variable, how do I do it?
Here is what I have tried so far:
set files [glob "./programming/.business_files/*.txt"]
set first_file $files[0]
I want to set the first file in my directory to a variable, how do I do it?
Here is what I have tried so far:
set files [glob "./programming/.business_files/*.txt"]
set first_file $files[0]
Your line with glob is right, but the order is not determined (it's whatever the OS system calls spit it out in, and those guarantee nothing about order in general). Thus, you'll probably want to lsort the list of filenames. The -dictionary option is highly recommended for this; it produces the order that users usually think of as the natural order of filenames.
set files [lsort -dictionary [glob "./programming/.business_files/*.txt"]]
Then you can get the first one with lindex (because Tcl uses […] for command substitutions, not indexing).
set first_file [lindex $files 0]
If you want to keep (for some reason) the array approach, a slightly less than ideal solution is:
set n 0
foreach file [lsort -dictionary [glob "./programming/.business_files/*.txt"]] {
set files($n) $file
incr n
}
set first_file $files(0)
That said, the lindex solution above is the cleanest.
Bye