简体   繁体   中英

PowerShell list files from remote SFTP server from wildcard subdirectories using WinSCP

I'm working with WinSCP in PowerShell to list files from wildcard subdirectories. However the $remotepath was not working, if I put wildcard/mask in between the subdirectories.

This is what I have so far:

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
}

Indeed, you can use a wildcard in the last path component only.

To implement what you need, you have to first list all folders in /ftpdata . Then loop over the folder list and list files in its infile/$currmon sub-subfolder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM