【问题标题】:PowerShell list files from remote SFTP server from wildcard subdirectories using WinSCPPowerShell 使用 WinSCP 从通配符子目录中列出远程 SFTP 服务器中的文件
【发布时间】:2020-02-12 04:04:37
【问题描述】:

我正在 PowerShell 中使用 WinSCP 列出通配符子目录中的文件。 但是,如果我将通配符/掩码放在子目录之间,$remotepath 将无法正常工作。

这是我目前所拥有的:

param (
    ##not working with mask, need to put full path
    $remotePath = "/ftpdata/*/infile/$currmon/",   
    $wildcard = "*$yesterday.*"
)

try
{   Add-Type -Path "WinSCPnet.dll"

    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "Hostnamme" UserName = "UserName" Password = "Password"
        SshHostKeyFingerprint = "ssh-rsa 2048 ............................"
    } 
    $session = New-Object WinSCP.Session

    try
    {   $session.Open($sessionOptions)
        $files = $session.EnumerateRemoteFiles(
            $remotePath, $wildcard, [WinSCP.EnumerationOptions]::None ) 

        if ($files.Count -gt 0)
        {
            foreach ($fileInfo in $files)
            {
                Write-Host ("$($fileInfo.Name) with size $($fileInfo.Length), " +
                    "last modification at $($fileInfo.LastWriteTime)")
            }
        }
        else
        {
            Write-Host "No files matching $wildcard found"
        }    
    }
    finally
    {
        $session.Dispose()
    }

    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

【问题讨论】:

    标签: powershell sftp wildcard winscp winscp-net


    【解决方案1】:

    确实,您只能在最后一个路径组件中使用通配符。

    要实现您所需要的,您必须首先列出/ftpdata 中的所有文件夹。然后遍历文件夹列表并列出其infile/$currmon 子文件夹中的文件。

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2019-08-30
      • 2018-03-21
      • 1970-01-01
      相关资源
      最近更新 更多