开发者

Tcl - Monitor FTP Upload percentage

开发者 https://www.devze.com 2023-04-12 15:45 出处:网络
How can I show the percentage of the upload handled by ::ftp::Put in Tcl? Example : proc upload {host user pass dir fileList} {

How can I show the percentage of the upload handled by ::ftp::Put in Tcl?

Example :

proc upload {host user pass dir fileList} {
      set handle [::ftp::Open $host $user $pass]

     ftpGoToDir $handle $dir
      # some counters for our feedback string
      set j 1
      set k [llength $fileList]

      foreach i $fileList {
        upload:status "uploading ($j/$k) $i"
      开发者_如何学运维  ::ftp::Put $handle $i
        incr j
}

Thanks :)


Here's one way (untested):

proc upload {host user pass dir fileList} {
    set handle [::ftp::Open $host $user $pass]

    ftpGoToDir $handle $dir
    # some counters for our feedback string
    set j 1
    set k [llength $fileList]

    foreach i $fileList {
        upload:status "uploading ($j/$k) $i"
        upload:execute $handle $i
        incr j
    }
}

proc upload:execute {handle filename {chunksize 8196}} {
    set filesize [file size $filename]
    set sent 0
    set fid [open $filename r]

    Put_or_Append Put $handle $fid $chunksize $filename $filesize sent
    while {![eof $fid]} {
        Put_or_Append Append $handle $fid $chunksize $filename $filesize sent
    }
    close $fid
}

proc Put_or_Append {cmd handle fid chunksize filename filesize sentvar} {
    set chunk [read $fid $chunksize]
    ::ftp::$cmd $handle -data $chunk $filename
    upvar 1 $sentvar sent
    incr sent [string length $chunk]
    puts [format "sent %d bytes (%d%%)" $sent [expr {100*$sent/$filesize}]]
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号