Post a reply

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

martin

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

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.
martin

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?
Uli

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.
martin

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)

See https://winscp.net/eng/docs/file_mask
Uli

Download files and move after download on to the remote server

Hi
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
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # 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)
            }
            else
            {
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Thanks