2011-10-13 87 views
1

如何顯示::ftp::Put處理的上傳百分比TclTcl - 監控FTP上傳百分比

例子:

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 
} 

謝謝:)

回答

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

它給我這個錯誤:'錯誤#ARGS:應該是「PROC名ARGS body「,同時執行」proc upload:execute handle filename {chunksize 8196}「'。對不起,我是Tcl的新手:) – yoda

+0

我錯過了proc身體的大括號。固定。我確實說過未經測試;) –

+0

謝謝。它仍然有一個ftp關閉方法的問題,不知道是怎麼一回事,看不到你的腳本中正在關閉的任何ftp指針。 – yoda