Re: Recursive proc/check file size



Kevin Walzer wrote:
I'm trying to recursively run a procedure to check a file size and then end when the file size stops increasing. Here is my code:

proc checksize {myfile} {

set filesize [file size $myfile]
lappend sizelist $filesize

puts $sizelist

if {$filesize > 0 && [lindex $sizelist end] == [lindex $sizelist end-1]} {
puts "all done"
}

after 1000
checksize $myfile
}
[...]

It appears that a single-item list is being returned each time, and so the comparison I am looking for (comparing the last item of the list with the next to last item, to make sure that the file size has stopped increasing) never gets run. Can anyone show me how to structure the procedure so that each iteration of the procedure adds to the list, rather than replacing it?

If you only need to check the previous size, then you don't need a list -- just an extra parameter. Also, if you combine the last two lines then you can use the event loop so that (a) you don't keep consuming stack space which each recursive call, and (b) you can potentially do other tasks while this loop is running:

proc checksize {myfile {prevsize 0}} {
set filesize [file size $myfile]
puts $filesize
if {$filesize > 0 && $filesize == $prevsize} {
puts "all done"
} else {
after 1000 [list checksize $myfile $filesize]
}
}
....
checksize $myfile
vwait forever ;# not needed if Tk is running

-- Neil
.



Relevant Pages

  • Re: Recursive proc/check file size
    ... proc checksize { ... set filesize [file size $myfile] ... puts "all done" ...
    (comp.lang.tcl)
  • Re: Recursive proc/check file size
    ... set filesize [file size $myfile] ... puts "all done" ...
    (comp.lang.tcl)
  • Re: Recursive proc/check file size
    ... set filesize [file size $myfile] ... If you're just learning tcl I can understand avoiding the event loop, but I still see no reason for a recursive solution that could, at least in theory, blow up if the recursion goes too deep. ...
    (comp.lang.tcl)
  • Recursive proc/check file size
    ... I'm trying to recursively run a procedure to check a file size and then end when the file size stops increasing. ... proc checksize { ... set filesize [file size $myfile] ... Kevin Walzer ...
    (comp.lang.tcl)
  • Re: reading file after a particular line in file
    ... File.foreach "myfile" do |line| ... puts line ... the first expression returns true and stays true until the last ... coud that trick be used for start and stop tags? ...
    (comp.lang.ruby)