0

There are few files in a directory like as1.1.log, df1.1.txt, gh1.1.bin, etc. The only common thing between the files is the version 1.1. Is there a possibility to add the list of files to a TCL list and check the existence of that list contents in the directory.

If the files are named like as50.1.1.log, as55.1.1.log, where 50 and 55 are two digit model numbers, can the file list be made like this? set version "1.1" set fileList {as??.$version.log, df$version.txt, gh$version.bin}

The above way of forming the list doesn't seem to work. Any suggestions?

Thanks in advance. -Ashwhin.

user2902205
  • 69
  • 2
  • 8
  • I'm not sure what you're asking for exactly. You're taking those files from directory X and then checking if they exist in directory X...? – Jerry Mar 17 '14 at 08:57
  • Also, any update on your [previous question](http://stackoverflow.com/q/19489061/1578604)? – Jerry Mar 17 '14 at 08:59

2 Answers2

0
set files [glob -directory "yourDirectoryName" *] ;#returns all names of files in the folder 
Omsai Jadhav
  • 134
  • 10
0

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!

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • The `glob` command makes it a bit easy to write what looks rather like magic incantations for all their comprehensibility. (OK, not _quite_ as bad as `lsearch` — which might as well be `lkitchensink` these days — but close.) I prefer the direct approach, given that both are going to have the problems with race conditions between what's creating the files and what is looking for them. (Also, consider also checking with `file isfile` instead of `file exists`.) – Donal Fellows Mar 17 '14 at 09:47
  • Can the list me made more generic like moving the version to a variable. set version "1.1" set files {as$version.log df$version.txt gh$version.bin} This doesn't seem to work. – user2902205 Mar 25 '14 at 14:34