Tcl - 递归步行和FTP上传

时间:2011-10-13 17:06:02

标签: recursion ftp tcl

如何在本地文件夹中进行递归遍历,以便将其中的所有内容上传到所需的ftp文件夹?这是我到目前为止所做的:

package require ftp

set host **
set user **
set pass **
set ftpdirectory **
set localdirectory **

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
}

  ::ftp::Close $handle
 }

 #---------------
 # feedback
 #---------------
 proc upload:status {msg} {
  puts $msg
 }

 #---------------
 # go to directory in server
 #---------------

 proc ftpGoToDir {handle path} {
     ::ftp::Cd $handle /
     foreach dir [file split $path] {
     if {![::ftp::Cd $handle $dir]} {
         ::ftp::MkDir $handle $dir
         ::ftp::Cd $handle $dir
     }
     }
 }

proc watchDirChange {dir intv {script {}} {lastMTime {}}} {
      set nowMTime [file mtime $dir]
      if [string eq $lastMTime ""] {
         set lastMTime $nowMTime
      } elseif {$nowMTime != $lastMTime} {
         # synchronous execution, so no other after event may fire in between
         catch {uplevel #0 $script}
         set lastMTime $nowMTime
      }
      after $intv [list watchDirChange $dir $intv $script $lastMTime]
 }

watchDirChange $localdirectory 5000 {
    puts stdout {Directory $localdirectory changed!}
    upload $host $user $pass $ftpdirectory [glob -directory $localdirectory -nocomplain *]
}

 vwait forever

提前致谢:)

1 个答案:

答案 0 :(得分:4)

您已经在使用ftp软件包,这意味着您已经安装了tcllib。好。这意味着您也可以获得fileutil包,并且可以执行此操作:

package require fileutil

# How to do the testing; I'm assuming you only want to upload real files
proc isFile f {
    return [file isfile $f]
}
set filesToUpload [fileutil::find $dirToSearchFrom isFile]

fileutil::find命令非常类似于递归glob,除了您将过滤器指定为命令而不是通过选项。

您可能更喜欢使用rsync;它不是Tcl命令,但它非常好,它将最小化实际传输的数据量。