Post a reply

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)


Topic review


$folders = $session.EnumerateRemoteFiles(
    $remotepath, $Null, [WinSCP.EnumerationOptions]::MatchDirectories)
foreach ($folder in $folders)
    if ($folder.Name -ne "backup")
            [WinSCP.RemotePath]::Combine($backuppath, $folder.Name))

I know exactly when dates are added (at night).
After this time I download the data and check the completeness. I could then start a second process that moves the data from the root directory to the backup directory. Do you have an example script for this?
There are only folders in the root directory. No files.

That's a very different operation than your current code does.
It is also hard to implement in an atomic way. What if some process adds the file to the folders between the moments when you (1) download and (2) move the folders?
Is that a risk you can take? Or avoid (e.g. by running your PowerShell script at a time, when no files can be added)?
Are there any files in the root folder?

Martin, thanks for the quick answer!
How I can skip the folder backup was one of my questions.
After downloading the data, how can I move the data from the root directory to the backup directory while keeping the folder structure? For example, there are two folders, in the root directory and there are several files in them. These 2 folders should be moved to the backup directory after downloading, including all files in them. The files should remain in the folders.

Re: Download files and move after download on to the remote server

If your question is how to exclude the backup folder, then the following should do:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "| backup/"
$transferResult =
    $session.GetFiles($remotepath, $localpath, $False, $transferOptions)


Download files and move after download on to the remote server

I want to download data and then move it on the remote server. I want to keep the folder structure. And exclude the backupfolder for downloading
I used this example:
Moving local files to different location after successful upload
This is my code:
$localpath = "C:\Temp\Download\"
$remotepath = '//*'
$remotepathbackup = '/backup/'
$session = New-Object WinSCP.Session
        # Connect
        # Download files, collect results
        $transferResult = $session.GetFiles($remotepath, $localpath)
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
            # Success or error?
            if ($transfer.Error -eq $Null)
                Write-Host "Download of $($transfer.FileName) succeeded, moving to backup"
                # Upload succeeded, move source file to backup
                $session.MoveFile(($transfer.FileName), $remotepathbackup)
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
        # Disconnect, clean up
    exit 0
    Write-Host "Error: $($_.Exception.Message)"
    exit 1