Version 6 of file mkdir

Updated 2005-04-22 17:14:58 by lwv

file mkdir dir ?dir ...?

Creates each directory specified. For each pathname dir specified, this command will create all non-existing parent directories as well as dir itself. If an existing directory is specified, then no action is taken and no error is returned. Trying to overwrite an existing file with a directory will result in an error. Arguments are processed in the order specified, halting at the first error, if any.


Csan When and how would it be possible to rewrite the following code into pure and simple Tcl while keeping the atomicity of the Unix mkdir?:

 set unid $A
 while {[catch {exec mkdir uniqueids/$unid}]} {
     if {[incr unid]>$B} {set unid $A}
 } 
 puts "$unid is available and has been locked."

The problem the above code is a solution for is: "Select the first available id between $A and $B. 'Available' is defined as 'not used by others'. When found, make sure you also lock it. When not used any more, unlock it."

The best Tcl solution for the problem would be to have negative feedback when trying to create an already existing directory. E.g.:

  file mkdir -booleanresult uniqueids/$unid

(or similar) which returned a boolean value depending on the success or failure of the directory creation would be a very good (best) improvement over the original code above. (Note that '-booleanresult' is only one of the possible names for the switch).

RS: How about this variation?

 # Assumptions:
 # A contains an integer value representing the low end of a range
 # B contains an integer value representing the high end of a range
 # Code rotates between A and B, looking for a directory that does not currently exist

 set unid $A
 while {[file exists uniqueids/$unid]} {
     if {[incr unid]>$B} {set unid $A}
 }
 file mkdir uniqueids/$unid
 puts "$unid is available and has been locked."

See also:


Tcl syntax help - Category Command