phantom2000
Jan 04, 2022Copper Contributor
Get file name from FTP Server
Hello,
I am using below code to query an FTP folder and get the name of the existing file. However, I am not getting the desired output. I get some unwanted results from the code. I appreciate some help in getting the file name only.
Code:
$username='Th1sUser'
$password='Th1sPas3'
$ftpuri='ftp://repo.ftpserver.com/Installer'
$uri=[system.URI] $ftpuri
$ftprequest=[system.net.ftpwebrequest]::Create($uri)
$ftprequest.Credentials=New-Object System.Net.NetworkCredential($username,$password)
$ftprequest.Method=[system.net.WebRequestMethods+ftp]::ListDirectory
$response=$ftprequest.GetResponse()
$strm=$response.GetResponseStream()
$reader=New-Object System.IO.StreamReader($strm,'UTF-8')
$list=$reader.ReadToEnd()
Write-Host "$list"
Result:
Installer/.
Installer/..
Installer/Installerv1.exe
I would like to get the installerv1.exe as the file name. Also is there a possibility to get the most recent .exe name from that directory and get that file name? Thank you in advance.