Welcome, guest | Sign In | My Account | Store | Cart

Recursively FTP from top level directory down creating directory structure on remote FTP Server.

Tcl, 125 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#################################################
# FTP Cloudscape DB to remote server - Recursive. 
#################################################

puts "\n executing [info script]\n"

# make script drive independent.

set drive [lindex [file split [info nameofexecutable]] 0 ] 

package require ftp
package require Tclx

######################################
# Get FTP Connection.
######################################
proc getFtp { host logon password transferType} {
    
    set ftp::VERBOSE 1

    set ftp::DEBUG 1

    puts  "\n **** getFtp_proc **** \n"

    set ftphandle [ ftp::Open $host $logon $password ]

    ftp::Type $ftphandle $transferType 

    return $ftphandle
    
}
##################################################################
# List the directories and files in the local nested directories.
##################################################################
proc getFiles { baseDir } {

    puts  "\n **** getFiles proc **** \n"

   # extract the sub directories from the base directory 

   set fileList [ recursive_glob $baseDir * ]

   foreach e $fileList { 

       if { [ file isdirectory $e ] } {

           lappend dList $e 

       } else {

           lappend fList $e 

       }

   }

   return [ list $dList $fList ] 

}
###############################################################
# Create the local directory structure on the remote server.
###############################################################
proc ftpDirsFiles { ftphandle files } {

   puts  "\n **** ftpDirsFiles proc **** \n"

   # create the remote directories. 
   # paths must be relative.  
 
   foreach i [ lindex $files 0 ] {

       set rd [ eval file join [ join [ lrange [ file split $i  ] 1 end ] ] ]

       puts "\n local dir: $i"
       puts "\n remotedir: $rd"

       ::ftp::MkDir $ftphandle $rd

   }

   # ftp the files. 
   # paths must be absolute on local server and relative on remote server.  

   unset i

   foreach i [ lindex $files 1 ] {

       set rf [ eval file join [ join [ lrange [ file split $i  ] 1 end ] ] ]

       puts "\n local file: $i"
       puts "\n remote file $rf"

       ::ftp::Put $ftphandle $i $rf 

   }

}
######################################
# Main Control.
######################################

set localDir   [ file join $drive CloudScapeDatabasesBackups ]
set host       ftphost
set logon      yourlogon
set password   yourpassword

set transferType binary 

puts "\n localDir   = $localDir"

# get FTP connection

if { [ catch { getFtp $host $logon $password $transferType } r ] == 0 } {  

   puts "\n Connected to $host as $logon \n"  
   set ftpHandle $r 

} else {

   puts "\n Failed to connect to $host as $logon \n"  
   exit 1

}

ftpDirsFiles $ftpHandle [ getFiles $localDir ] 
Created by Patrick Finnegan on Tue, 6 Dec 2005 (MIT)
Tcl recipes (162)
Patrick Finnegan's recipes (56)

Required Modules

  • (none specified)

Other Information and Tasks