0

i want to search after a code which search a file called hw.exe on my hdd.

i have found this following code:

set files [glob hw.exe]
    set sofar -1

    foreach f $files {
            set size [file size $f]
            if {$size > $sofar} {
                    set sofar $size
                    set name $f
                    }
            }

    puts "Biggest files is $name at $sofar bytes"

Has anybody one idea to correct this? Or is there a function?

ManInTheMiddle
  • 128
  • 1
  • 2
  • 13
  • 1
    http://stackoverflow.com/questions/11104940/tcl-deep-recursive-file-search-search-for-files-with-c-extension – gavv Aug 10 '16 at 13:36

1 Answers1

2

The fileutil package in Tcllib has the capability that you're looking for, provided you write the right filter. Finding the largest file can then be done over the returned list, which ought to be far smaller than the list of all files on the drive!

package require fileutil

proc filter {name} {
    return [string match hw.exe $name]
}
set hwlist [fileutil::find C:/ filter]

set sofar -1
set name ""
foreach f $hwlist {
    set size [file size $f]
    if {$size > $sofar} {
        set name $f
        set sofar $size
    }
}
puts "Found $name of size $sofar"
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Hey Donal, thanks but i cant use the fileutil package. I have to use the regular tcl statements – ManInTheMiddle Aug 10 '16 at 13:31
  • That use of `string match` actually is just an equality, but it's easy to adapt to being more flexible if required. Also, the algorithm for finding the maximum size _given a list of files_ from the question is pretty much as good as it gets; maximums require a linear scan. – Donal Fellows Aug 10 '16 at 13:32
  • No fileutil package? Boo! Just copy the code from http://core.tcl.tk/tcllib/artifact/4709390bcbd91507 (it's under the same license as Tcl). – Donal Fellows Aug 10 '16 at 13:34