我有一个调用 WinSCP .NET 程序集的脚本。该脚本从 FTP 目录下载最新文件,并根据文件扩展名 + .txt
( 2245.xml
-> xml.txt
) 命名它们。
我需要创建一个过滤器来仅下载名为tn*
or的文件扩展名nc1
。谁能指出我正确的方向:
$session = New-Object WinSCP.Session
# Connect
$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} |
Group-Object { [System.IO.Path]::GetExtension($_.Name) } |
ForEach-Object{
$_.Group | Sort-Object LastWriteTime -Descending | Select -First 1
}
$extension = [System.IO.Path]::GetExtension($latest.Name)
"GetExtension('{0}') returns '{1}'" -f $fileName, $extension
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}
# Download
$latest | ForEach-Object {
$extension = ([System.IO.Path]::GetExtension($_.Name)).Trim(".")
$session.GetFiles($session.EscapeFileMask($remotePath + $_.Name), "$localPath\$extension.txt" ).Check()
}
我尝试在目录排序中添加一个过滤器,但没有奏效:
Where-Object { -Not $_.IsDirectory -or [System.IO.Path]::GetExtension($_.Name) -like "tn*" -or [System.IO.Path]::GetExtension($_.Name) -eq "nc1"} |
谢谢!