Hello everybody I've been strugglling for many days now with this, I would like to get some help. What I am trying to do is to download the latest files from FTP server but what I've got here is going to download all the files from the ftp server !!! I don't know how to add condition in a powershell script I'm a newbie to this. Gonna drop what I have for now just here.
So please let me know if any additional information is required and I will provide it immediately, thanks again for your help.
`Write-Host "Please wait while your file downloads"
#Function to get all files
function Get-FtpDir ($url,$credentials)
{
$request = [Net.WebRequest]::Create($url)
$request.Credentials = $credentials
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream()
$readline = $reader.ReadLine()
$output = New-Object System.Collections.Generic.List[System.Object]
while ($readline -ne $null)
{
$output.Add($readline)
$readline = $reader.ReadLine()
}
$reader.Close()
$response.Close()
$output
}
$server = 'ftp.xxxx.xxx'
$user = 'xxxxx'
$pass = 'xxxxxxxxx'
$localfilelocation = 'C:\Users\Lala\Desktop\test\'
$uri = New-Object System.Uri("ftp://$server/")
#List of all files on FTP-Server
$files = Get-FTPDir $uri -credentials (New-Object System.Net.NetworkCredential($user, $pass))
foreach ($file in $files)
{
if ($file)
{
$file
$path = "$localfilelocation\$file"
$fileuri = New-Object System.Uri("ftp://$server/$file")
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
$webclient.DownloadFile($fileuri,$path )
}
}