【问题标题】:PowerShell WinSCP FTP script is downloading cached file for only 1 out of 8 filesPowerShell WinSCP FTP 脚本仅下载 8 个文件中的 1 个的缓存文件
【发布时间】:2016-04-11 17:12:12
【问题描述】:

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

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

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

我已经将我的 IE 选项设置为“每次访问网页时检查存储页面的较新版本”。

我正在从 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
}

【问题讨论】:

  • 设置Session.SessionLogFile 并附加一个显示tn5 文件下载的日志。
  • 标题说“缓存文件仅用于 8 个文件中的 1 个”,而文本说 “创建日期为 2015 年 12 月”。目前尚不清楚问题是否只是时间戳,或者文件的内容是否也是旧的。如果是后者,您可以使用任何(GUI)FTP 客户端下载文件的最新版本吗?
  • 文件的时间戳和内容都是旧的,这让我相信我的计算机或者托管我的“H”驱动器的服务器正在加载文件的缓存版本。我会努力获取日志。
  • 您是否尝试在其他地方下载文件?
  • 我只是使用 WINSCP GUI 完全按照我的脚本下载它并且它运行良好。然后我删除了文件并从powershell运行了powershell脚本,它工作了。我又把文件删了。最后,我使用 VBA 调用了脚本,它下载了正确的文件,但一秒钟后它被旧版本的文件替换了!我看着它发生。很奇怪。

标签: windows powershell ftp winscp winscp-net


【解决方案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()

【讨论】:

    猜你喜欢
    • 2012-02-02
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2022-01-07
    • 2018-02-16
    • 2015-10-25
    • 2016-05-27
    相关资源
    最近更新 更多