posts - 598 , comments - 849 , trackbacks - 247

My Links

News

qrcode

Lance Robinson

Create Your Badge

Lance Robinson is a software engineer in Durham, Chapel Hill, Raleigh, and surrounding areas. More about Lance.

 Subscribe

Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

Blogs

Miscellanous

Noteworthy Stuff

Popular Posts

NetCmdlets FTP - recursive directory upload

update:  here's an updated function for uploading an entire directory tree:

## ftprecursiveupload.ps1: Recursive FTP Upload
## Uploads a directory tree to a remote FTP server.
## Returns an objects containing information about the files transferred.
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
}
}
}

Original Post:

ckj asked me how to "recursively upload a directory mutiple levels deep via send-ftp". Here is my answer:

For now, the send-ftp cmdlet allows you to upload one file at a time (its a beta). But you can easily use get-children's recursive flag to get a list of all the files to upload, and call send-ftp for each one. Here is a little script to do so.


param( [string] $dir = "C:\Testing\FTPTest\" )

$files = (get-childitem $dir -r)
foreach ($file in $files) {
$remfilename = $file.FullName.Replace($dir, "")
$remfilename = $remfilename.Replace("\", "/")
if ($file.Attributes -eq "Directory") {
send-ftp -server MYSERVER -user TEST -password TEST -create $remfilename
}
else {
send-ftp -server MYSERVER -user TEST -password TEST -localfile $file.FullName -remotefile $remfilename
}
Write-Host $remfilename
}

Technorati : , , ,

Print | posted on Thursday, December 7, 2006 11:37 AM | Filed Under [ Programming Software PowerShell ]

Feedback

Gravatar

# re: NetCmdlets FTP - recursive directory upload

How could I check if the directory already exists before creating it?
9/17/2007 11:34 AM | Recurfizz
Gravatar

# re: NetCmdlets FTP - recursive directory upload

The command:

get-ftp -server MYSERVER -user TEST -password TEST

Will return a directory listing.
9/17/2007 3:12 PM | Lance
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: