PowerShell WinSCP FTP 脚本正在为 8 个文件中的 1 个下载缓存文件

PowerShell WinSCP FTP script is downloading cached file for only 1 out of 8 files

我有一个 PowerShell 脚本可以下载特定文件扩展名的最新文件。 FTP 目录中有数百个每小时时间戳文件 (mmddhh),它会在每小时结束时清除这些文件。每个文件都有一个唯一的扩展名。我每小时下载扩展名 .tn1.tn2.tn3.tn4.tn5.ky1.nc1 的文件.

文件在本地保存为 extension.txt(例如,tn1.txttn2.txt 等)。

我遇到的问题是下载的 tn5 文件的创建日期是 2015 年 12 月,但在服务器上它是最新的(2016 年 4 月)。

我已经将我的 IE 选项设置为“检查存储页面的更新版本”"every time I visit the webpage"。

我正在执行来自 VBA 的脚本:

Shell("powershell ""H:\Worksheets\FTP\FTP.ps1""", vbHide)
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
    $localPath = "H:\Worksheets\FTP"
    $remotePath = "/outgoing/data/LatestData/"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::ftp
    $sessionOptions.HostName = 
    $sessionOptions.UserName = 
    $sessionOptions.Password = 

    $session = New-Object WinSCP.Session

    try
    {
        # 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} |
            Where-Object {
                [System.IO.Path]::GetExtension($_.Name) -eq ".nc1" -or
                [System.IO.Path]::GetExtension($_.Name) -eq ".ky1" -or
                [System.IO.Path]::GetExtension($_.Name) -like ".tn*" }

        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
        }

        $latest | ForEach-Object{
            $extension = ([System.IO.Path]::GetExtension($_.Name)).Trim(".")
            $sourcePath = [WinSCP.RemotePath]::EscapeFileMask($remotePath + $_.Name)
            $session.GetFiles($sourcePath, "$localPath$extension.txt" ).Check()
        }

        $stamp = $(Get-Date -f "yyyy-MM-dd-HHmm")
        $filename = $stamp.subString(0,$stamp.length-6)
        $session.GetFiles(
            ($remotePath + $fileName),
            ($localPath + $fileName + "." + $stamp)).Check()
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}

目标文件名由GetFiles方法调用的第二个参数指定,即:

中的"$localPath$extension.txt"
$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($remotePath + $_.Name)
$session.GetFiles($sourcePath, "$localPath$extension.txt").Check()

如果要将“1”附加到基本名称,请使用 "$localPath${extension}1.txt":

$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($remotePath + $_.Name)
$session.GetFiles($sourcePath, "$localPath${extension}1.txt").Check()