Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

JS973

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