PoshCode Archive  Artifact [eb9620a249]

Artifact eb9620a249a78ba27334369e872a5e4fee4ca096a39919cd42a06b4aa2f0e29f:

  • File FTP-Upload-Dir-Tree.ps1 — part of check-in [0d903dbbaa] at 2018-06-10 12:56:31 on branch trunk — Uploads a directory tree to a remote FTP server. Uses NetCmdlets (send-ftp). (user: Lance Robinson size: 1366)

# encoding: ascii
# api: powershell
# title: FTP Upload Dir Tree
# description: Uploads a directory tree to a remote FTP server.  Uses NetCmdlets (send-ftp).
# version: 0.1
# type: function
# author: Lance Robinson
# license: CC0
# function: upload-directory
# x-poshcode-id: 1140
# x-archived: 2017-03-23T21:10:32
# x-published: 2010-06-01T09:11:00
#
#
function upload-directory {
  param( [string] $server = $( Throw "You must specify an FTP server to logon to."),
	 [string] $dir = $( Throw "You must specify a local directory to upload (ie, C:\Testing\FTPTest\)"),
	 [switch] $overwrite = $false,
	 [System.Management.Automation.PSCredential] $cred = $( Throw "You must provide credentials with which to logon to the FTP server.") ) 
        
  $files = (get-childitem $dir -r)

  foreach ($file in $files) {
    $remfilename = $file.FullName.Replace($dir, "")
    $remfilename = $remfilename.Replace("\", "/")
    if ($file.Attributes.ToString().IndexOf("Directory") -ge 0) {
  	  try
  	  {
      send-ftp -server $server -cred $cred -create $remfilename -overwrite:$overwrite
      }
      catch {} #if the directory already exists, ignore the error
    }
    else {
      send-ftp -server $server -cred $cred -localfile $file.FullName -remotefile $remfilename -overwrite:$overwrite
    } #end ifelse
  } #end foreach

}