Wednesday, 30 March 2011

tcl file_copy_rsync proc

###########################################
# Name:   file_copy_rsync
#
# Inputs: sFromFile:    path and file name to copy from
#         sToFileOrDir: path (or path and file name) to copy file to
#
# Output: file copied or not, puts info
#
# Desc:   tcl rsync-style file copy,
#         quite a bit more crude than proper rsync
#         copy files only if size or mtime has changed
#         substitute for file copy (or file copy -force) when log files are copied multiple times
#
# test and usage file copy / file rsync
#  set sToDir "P:/destdir/d/d"
#  set sFromDir "A:/fromdir/foo"
#  set sLogFile test_30_03_11_09_24_36.log
#  file_copy_rsync ${sFromDir}/$sLogFile ${sToDir}/Logs
#  file_copy_rsync ${sFromDir}/$sLogFile ${sToDir}/Logs/
#  file_copy_rsync ${sFromDir}/$sLogFile ${sToDir}/Logs/$sLogFile
#  #file copy -force ${sFromDir}/$sLogFile ${sToDir}/Logs/
#
# Author: James Coleman, 30/3/2011
#
###########################################
proc file_copy_rsync {sFromFile sToFileOrDir} {

  if {[file isdirectory $sToFileOrDir]} {
    # get base file name and set sToFile to be full dir path + dest file name
    set sFromFileBaseName [string range $sFromFile [expr [string last "/" $sFromFile] + 1]  end]
    set sToFile $sToFileOrDir/$sFromFileBaseName
    puts "sToFile=$sToFile"
  } else {
    set sToFile $sToFileOrDir
  }

  set iFromSize [file size $sFromFile]
  #puts "from size:$iFromSize"
  file stat $sFromFile aFromStat
  #parray aFromStat
  catch {
    set iToSize [file size $sToFile]
    #puts "to size:$iToSize"
    file stat $sToFile aToStat
    #parray aToStat
  } sError
  #puts "sError:$sError"
  if {"$sError" != "" || $iFromSize != $iToSize || $aFromStat(mtime) != $aToStat(mtime) } {
     #puts "DO FILE COPY"
     file copy -force $sFromFile $sToFile
  } else {
     puts "Not doing FILE COPY, size $iFromSize == $iToSize, mtime $aFromStat(mtime) == $aToStat(mtime), err $sError"
  }
}




The real rsync is here: http://rsync.samba.org/

No comments: