Get file name from FTP Server

Copper Contributor



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. 






$uri=[system.URI] $ftpuri

$ftprequest.Credentials=New-Object System.Net.NetworkCredential($username,$password)


$reader=New-Object System.IO.StreamReader($strm,'UTF-8')

Write-Host "$list"









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. 

4 Replies



Tried it myself, used the $ftprequest.Method=[]::ListDirectoryDetails to get timestamps but not easy to parse those. Then I read a post ( about using WinSCP as client which gives you more options. Also a link below in the same article with even more  code 




# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = ""
    UserName = "user"
    Password = "mypassword"

$session = New-Object WinSCP.Session

# Connect

# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)

# Select the most recent file
$latest =
    $directoryInfo.Files |
    Where-Object { -Not $_.IsDirectory } |
    Sort-Object LastWriteTime -Descending |
    Select-Object -First 1

# Any file at all?
if ($latest -eq $Null)
    Write-Host "No file found"
    exit 1

# Download the selected file
$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($remotePath + $latest.Name)
$session.GetFiles($sourcePath, $localPath).Check()



This piece of code was written by the author of WinSCP (Martin Prikryl ) , so I know it's good :) 


you could also try using split as well to split out the filename from the path. I use the below script in my scripts to create a batch name using the filename without the extension



$BatchNameTemp      = $Script:File.split("\")[-1]
$Script:BatchName   = $BatchNameTemp.substring(0,($BatchNameTemp.length-4))


so you could modify your script to 


$FName = $list.split("/")[-1]




Did you manage to fix your issue?


It's well worth using split-path to separate files names and file paths..

# to get the file name
split-path "C:\windows\system32\notepad.exe" -Leaf

# result
# notepad.exe

# to get the directory
split-path "C:\windows\system32\notepad.exe" -Parent

# result
# C:\windows\system32