使用这个命令我可以在 Unix 中获取最新更新的文件夹
ls -t1 | head -1
但是我怎样才能从 Windows 的 FTP 服务器中获取相同的内容呢?
我想在 FTP 服务器的特定路径上获取最新更新的文件夹的名称。任何人都可以帮忙吗?
最佳答案
使用 Windows shell 命令没有简单的方法可以做到这一点。
您可以:
- 使用
ftp.exe
执行ls/path c:\local\path\listing.txt
将目录列表保存到文本文件。 - 退出
ftp.exe
。 - 解析列表并找到最新文件。对于 Windows shell 命令来说,这不是一件容易的事。
使用 PowerShell 脚本会更容易。
您可以使用 FtpWebRequest
class .尽管它也没有一种简单的方法来检索结构化目录列表。它仅提供 ListDirectoryDetails
和 GetDateTimestamp
方法。
参见 Retrieving creation date of file (FTP) .
或者使用第 3 方库来完成任务。
例如 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 的作者)
关于windows - 我想在 FTP 服务器的特定路径获取最新更新文件夹的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31353857/