I have a PowerShell script which downloads all files from an FTP directory to a local folder (Thanks to Martin Prikryl).
The FTP directory is updated with multiple new files at various times throughout the day.
I will be running my script every 30 minutes via Task Scheduler to download files from the FTP directory.
I would like the script to check and only download new files from the FTP directory to local folder.
Note: The check must be done on the FTP directory as previously downloaded files may be removed from Local folder.
Please see below for my current script;
#FTP Server Information - SET VARIABLES
$user = 'user'
$pass = 'password'
$target = "C:\Users\Jaz\Desktop\FTPMultiDownload"
#SET FOLDER PATH
$folderPath = "ftp://ftp3.example.com/Jaz/Backup/"
#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)
function Get-FtpDir ($url,$credentials) {
$request = [Net.WebRequest]::Create($url)
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
if ($credentials) { $request.Credentials = $credentials }
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream()
$reader.ReadToEnd()
$reader.Close()
$response.Close()
}
$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
$files = ($Allfiles -split "`n")
$files
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$counter = 0
foreach ($file in ($files | where {$_ -like "*.*"})){
$source=$folderPath + $file
$destination = Join-Path $target $file
$webclient.DownloadFile($source, $destination)
#PRINT FILE NAME AND COUNTER
$counter++
$counter
$source
}
Thanks in advance for you help!