To check if all the files named in a list are present, use a little helper procedure (because helper procedures make things much clearer):
proc allPresent {fileList} {
foreach f $fileList {
if {![file exists $f]} {
return false
}
}
return true
}
set files {as1.1.log df1.1.txt gh1.1.bin}
if {[allPresent $files]} {
puts "Everything here"
} else {
puts "Something absent"
# Returning a boolean does mean that you don't know *which* is absent...
}
Those filenames probably should be fully-qualified anyway. (That's good practice because it means that your code isn't so dependent on the value of pwd
.) If they aren't, you can qualify them as you go with file join
, as shown in this adapted version...
# Note! Optional second argument
proc allPresent {fileList {directory .}} {
foreach f $fileList {
if {![file exists [file join $directory $f]]} {
return false
}
}
return true
}
if {[allPresent $files that/directory]} {
...
Another approach is to glob
to get all the filenames and then do a check against that:
proc allPresent {fileList {directory .}} {
# Glob pattern might be better as *1.1*
set present [glob -nocomplain -directory $directory -tails *]
foreach f $fileList {
if {$f ni $present} {
return false
}
}
return true
}
But that's not really much more efficient in practice. Might as well write it out as clearly as possible!