My script downloads a hardcoded file name with its data from an ftp site, but I need it to download the newest files and (perhaps by last downloaded timestamp). I just started using powershell and have searched for clues to make my script more sophisticated, but cant seem to get most of the code Ive found to work. Can you give me some direction? This is my current code:
# Create FTP Connection
$FTPRequest = [System.Net.FtpWebRequest]::Create("ftp://ftp.sitepath/newestfile.txt")
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential("username", "pw")
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UsePassive = $false
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
$folderName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
New-Item -itemType Directory -Path \\server\directory\ -Name $FolderName
$targetfile = New-Object
IO.FileStream "\\server\directory\$FolderName\newestfilename.txt"), [IO.FileMode]::Create)
# Get FTP File
$FTPResponse = $FTPRequest.GetResponse()
$ResponseStream = $FTPResponse.GetResponseStream()
$FTPReader = New-Object -typename System.IO.StreamReader -ArgumentList $ResponseStream
[byte[]]$readbuffer = New-Object byte[] 1024
#loop through the download stream and send the data to the target file
do{
$readlength = $ResponseStream.Read($readbuffer,0,1024)
$targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)
$FTPReader.Close()
$FTPReader.Close()
Thank you for your help.