【问题标题】:I want to fetch the name of the latest updated folder at particular path of FTP server我想在 FTP 服务器的特定路径中获取最新更新的文件夹的名称
【发布时间】:2015-07-11 05:43:48
【问题描述】:

使用此命令我可以在 Unix 中获取最新更新的文件夹

ls -t1 | head -1

但是我怎样才能从 Windows 的 FTP 服务器中获得相同的结果呢?

我想获取 FTP 服务器特定路径上最新更新的文件夹的名称。有人可以帮忙吗?

【问题讨论】:

  • 在什么平台上?你有什么 ftp 客户端可用?
  • 嗨马丁,它在 linux 平台(腻子)上。使用 shell 编程,我能够导航到 ftp 服务器的特定路径并获取文件。但是如何导航到 ftp 服务器特定路径的最新更新子文件夹并获取文件。我正在寻找登录 ftp 目录的 shell 命令,导航到我预先提供的路径并在其中获取最新更新的目录(子文件夹)
  • 我的意思是你的客户端平台是什么。不是什么是服务器平台。我假设您不在 Linux 上运行 PuTTY。它通常在 Windows 上运行以连接到 Linux 服务器。
  • 嗨,马丁,是的,Windows 是我的客户端平台。我在windows平台上运行putty来连接ftp服务器

标签: windows ftp


【解决方案1】:

使用 Windows shell 命令没有简单的方法来做到这一点。

你可以:

  • 使用ftp.exe 执行ls /path c:\local\path\listing.txt 将目录列表保存到文本文件中。
  • 退出ftp.exe
  • 解析列表并查找最新文件。对于 Windows shell 命令来说,这不是一件容易的事。

使用 PowerShell 脚本会更容易。

您可以使用FtpWebRequest class。尽管它也没有一种简单的方法来检索结构化目录列表。它仅提供ListDirectoryDetailsGetDateTimestamp 方法。

Retrieving creation date of file (FTP)


或者使用 3rd-party 库来完成任务。

例如使用WinSCP .NET assembly,您可以这样做:

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"
}

查看完整示例Downloading the most recent file

(我是 WinSCP 的作者)

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多