You don't need a variable, you have the number you need in the file list:
set no [scan [lindex [lsort -dictionary [glob log_file_*]] end] log_file_%2d]
incr no
set fn [open log_file_$no w]
Let's break that up a bit. Create a list of log files:
set filelist [glob log_file_*]
Sort the list in dictionary order (where 10 comes after 2) and pick the last element:
set highest_numbered [lindex [lsort -dictionary $filelist] end]]
Extract the number from the file name:
set no [scan $highest_numbered log_file_%2d]
Increase the number and open a new log file:
incr no
set fn [open log_file_$no w]
If there is a possibility that no log files exist, the glob command will fail. To take care of this case, either do this:
set filelist [glob -nocomplain log_file_*]
if {[llength $filelist]} {
set highest_numbered [lindex [lsort -dictionary $filelist] end]]
set no [scan $highest_numbered log_file_%2d]
} else {
set no 0
}
incr no
set fn [open log_file_$no w]
or this slightly safer version (if you have Tcl 8.6):
try {
glob log_file_*
} on ok filelist {
set highest_numbered [lindex [lsort -dictionary $filelist] end]
set no [scan $highest_numbered log_file_%2d]
} trap {TCL OPERATION GLOB NOMATCH} {} {
set no 0
} on error message {
puts stderr "error when listing log files: $message"
set no -1
}
if {$no > -1} {
incr no
set fn [open log_file_$no w]
# ...
chan close $fn
}
Documentation: chan, glob, if, incr, lindex, lsort, open, scan, set, try
(Note: the 'Hoodiecrow' mentioned elsewhere is me, I used that nick earlier.)