Tcl - 递归遍历和FTP上传

发布于 2024-12-10 00:13:11 字数 1578 浏览 0 评论 0原文

如何递归遍历本地文件夹,以便将其中的所有内容上传到所需的 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

提前致谢:)

How can I do a recursive walk through a local folder in order to upload everything it has inside it to the desired ftp folder? Here's what I have so far :

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

Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-12-17 00:13:11

您已经在使用 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 命令,但它非常好,它将最大限度地减少实际传输的数据量。

You're already using the ftp package, so that means you've got tcllib installed. Good. That means in turn that you've got the fileutil package as well, and can do this:

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]

The fileutil::find command is very much like a recursive glob, except that you specify the filter as a command instead of via options.

You might prefer to use rsync instead though; it's not a Tcl command, but it is very good and it will minimize the amount of data actually transferred.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文