Tcl - 监控FTP上传百分比

时间:2011-10-13 16:44:17

标签: ftp tcl

如何在 Tcl 中显示由::ftp::Put处理的上传百分比?

示例:

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
}

谢谢:)

1 个答案:

答案 0 :(得分:2)

这是一种方式(未经测试):

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}]]
}