2

Using this command I am able to get the latest updated folder in Unix

ls -t1 | head -1

But how can I get the same in FTP server from Windows?

I want to get the name of the latest updated folder at particular path of FTP server. Could any one please help?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • On what platform? What ftp client do you have available? – Martin Prikryl Jul 11 '15 at 07:43
  • Hi Martin, It's on linux platform(putty). Using shell programming, am able to navigate to the particular path of ftp server and fetch the files. But how can I navigate to the latest updated subfolder of particular path of ftp server and get the files. I am looking for shell command that logs into ftp directory, navigates to the path I provide in advance and fetch the latest updated directory(sub folder) in it – Achyuth Bharath Jul 12 '15 at 07:08
  • I mean what's your client platform. Not what is the server platform. I assume you do not run PuTTY on Linux. It's typically run on Windows to connect to Linux server. – Martin Prikryl Jul 12 '15 at 08:42
  • Hi Martin, yes windows is my client platform. I run putty on windows platform to connect to ftp server – Achyuth Bharath Jul 13 '15 at 07:25

1 Answers1

0

There's no easy way to do this with Windows shell commands.

You can:

  • Use ftp.exe to execute ls /path c:\local\path\listing.txt to save a directory listing to a text file.
  • Exit ftp.exe.
  • Parse the listing and find the latest files. Not an easy task for Windows shell commands.

It would be a way easier with a PowerShell script.

You can use FtpWebRequest class. Though it does not have an easy way to retrieve structured directory listing either. It offers only ListDirectoryDetails and GetDateTimestamp methods.

See Retrieving creation date of file (FTP).


Or use a 3rd-party library for the task.

For example with WinSCP .NET assembly you can do:

param (
    $sessionUrl = "ftp://user:mypassword@example.com/",
    $remotePath = "/path"
)

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

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl($sessionUrl)

# Connect
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)

# 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"
}
else
{
    Write-Host "The latest file is $latest"
}

See full example Downloading the most recent file.

(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992