I am writing a PowerShell script which will be executed at timely intervals, which will grab any content of a specific directory + sub-directories and upload a copy of it, to an FTP server.
In case file already exist on the server, it will overwrite.
In case the directory does not exist on the server, it will be created.
I have achieved most of what I need it to do but am stuck on the sub-directory creation if not exist part.
The code I have is this, although not really clean yet.
function UploadFilesInDirectory($RemoteDestination, $LocalSource) {
$local_files = Get-ChildItem $LocalSource
foreach ($local_file in $local_files)
{
$isDirectory = (Get-Item $local_file.FullName) -is [System.IO.DirectoryInfo]
if ($isDirectory)
{
UploadFilesInDirectory -RemoteDestination "$RemoteDestination" -LocalSource "$LocalSource\$local_file"
}
else
{
Write-Host "Uploading $local_file"
$desinationPath = $LocalSource.Replace("$local_backup\", "")
$desinationPath = "$remote_connection$desinationPath\$local_file".Replace("\", "/")
$webclient.UploadFile("$desinationPath", $file.FullName )
}
}
}
Any help will be appreciated.