Move files but exclude subdirectories from remote site

Advertisement

JS973
Guest

Move files but exclude subdirectories from remote site

Disclaimer: I'm pretty new to PowerShell and WinSCP so any help would be appreciated. I'd like to edit a PowerShell script to move all files (different file types) from a remote directory to a local folder, but not move any of the sub-directories and the files in them.

I've declared a few variables:
$ftpHostName = "server.mydomain.local"
$RemotePath = "/C/Newfolder/"
$wildcard = "*.*"
$LocalPath = "E:\Test\"
Portion of the script for file movement is below:
#Count files
$files = $session.EnumerateRemoteFiles($remotePath, $wildcard,[WinSCP.EnumerationOptions]::None)
 
$transferResult = $session.GetFiles("$RemotePath\$wildcard", $LocalPath, $False, $transferOptions)
 
#Start if
if ($files.Count -gt 0)
{
    foreach ($transfer in $transferResult.Transfers)
    {
        $logger.Info("****Download of $($transfer.FileName) succeeded****")
        Write-Host "Download of $($transfer.FileName) succeeded"
              
        $session.RemoveFiles( $transfer.FileName).null
    } 
}
else
{
    Write-Host "No files found"
    $logger.Info("****No files found****")
}
Issue is when using *.* all files and subdirectories are transferred across. The goal is to not have the subdirectories and files in the subdirectories moved, and to just have the files in the parent directory transferred. Is there a way to achieve this?

Thanks in advance

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum