Skip to content

Instantly share code, notes, and snippets.

@dkittell
Created November 24, 2015 18:13
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save dkittell/f029b6c7d1c46ebcffcb to your computer and use it in GitHub Desktop.
Save dkittell/f029b6c7d1c46ebcffcb to your computer and use it in GitHub Desktop.
PowerShell – FTP Upload Directory With Sub-Directories
clear
# FTP Server Variables
$FTPHost = 'ftp://192.168.1.1/html/'
$FTPUser = 'user'
$FTPPass = 'password'
#Directory where to find pictures to upload
$UploadFolder = "C:\Temp\"
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPass)
$SrcEntries = Get-ChildItem $UploadFolder -Recurse
$Srcfolders = $SrcEntries | Where-Object{$_.PSIsContainer}
$SrcFiles = $SrcEntries | Where-Object{!$_.PSIsContainer}
# Create FTP Directory/SubDirectory If Needed - Start
foreach($folder in $Srcfolders)
{
$SrcFolderPath = $UploadFolder -replace "\\","\\" -replace "\:","\:"
$DesFolder = $folder.Fullname -replace $SrcFolderPath,$FTPHost
$DesFolder = $DesFolder -replace "\\", "/"
# Write-Output $DesFolder
try
{
$makeDirectory = [System.Net.WebRequest]::Create($DesFolder);
$makeDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPass);
$makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory;
$makeDirectory.GetResponse();
#folder created successfully
}
catch [Net.WebException]
{
try {
#if there was an error returned, check if folder already existed on server
$checkDirectory = [System.Net.WebRequest]::Create($DesFolder);
$checkDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPass);
$checkDirectory.Method = [System.Net.WebRequestMethods+FTP]::PrintWorkingDirectory;
$response = $checkDirectory.GetResponse();
#folder already exists!
}
catch [Net.WebException] {
#if the folder didn't exist
}
}
}
# Create FTP Directory/SubDirectory If Needed - Stop
# Upload Files - Start
foreach($entry in $SrcFiles)
{
$SrcFullname = $entry.fullname
$SrcName = $entry.Name
$SrcFilePath = $UploadFolder -replace "\\","\\" -replace "\:","\:"
$DesFile = $SrcFullname -replace $SrcFilePath,$FTPHost
$DesFile = $DesFile -replace "\\", "/"
# Write-Output $DesFile
$uri = New-Object System.Uri($DesFile)
$webclient.UploadFile($uri, $SrcFullname)
}
# Upload Files - Stop
@Yelleko
Copy link

Yelleko commented Mar 20, 2017

I love this script. Was curious if there was an easy way to log the output of the files uploaded?

@nutt318
Copy link

nutt318 commented Mar 20, 2017

is it possible to create a new directory once connected and then put everything in that new folder? Not the greatest with powershell and trying to figure it out but no luck yet.

@TheUltimateC0der
Copy link

You Sir, saved me a shitload of time ! Thanks for this script !

@francesco1119
Copy link

francesco1119 commented May 27, 2018

Is there a way to keep PowerShell script running all day long so it can upload the any other files that is added to the directory?

@gabivigo
Copy link

Great script! Thanks for sharing David

@roshanlouhar
Copy link

I desperately looking for this man. Anyways Thnx man.

@Smilefounder
Copy link

Thanks a lot <3

@diaverso
Copy link

I have this error.

https://i.imgur.com/GLuC8PW.png

@dkittell
Copy link
Author

Apparently my notifications for gist are working again.

@diaverso , based on the error it looks like the function is wanting 2 parameters but not receiving 2

@francesco1119 , likely you have figured out a solution by now. Sorry notifications were not working for a long time. If you still need assistance feel free to respond. My initial thought would be to run a schedule task with the PowerShell script

@nutt318 , likely you have figured out a solution by now. But it is possible to do a unix-style rsync option to create directories as needed. If you haven't figured out a solution feel free to respond.

@Yelleko , likely you have figured out a solution as well by now. My initial thought would be to uncomment line 59 and add a pipe to output it to a file `Write-Output $DesFile | Out-File -Append -FilePath file-upload-log.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment