Download files and move after download on to the remote server

Advertisement

Uli
Joined:
Posts:
3
Location:
Germany

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: https://winscp.net/eng/docs/script_local_move_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

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
33,467
Location:
Prague, Czechia

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

Reply with quote

Uli
Joined:
Posts:
3
Location:
Germany

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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
33,467
Location:
Prague, Czechia

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?

Reply with quote

Uli
Joined:
Posts:
3
Location:
Germany

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.

Reply with quote

Advertisement

martin
Site Admin
martin avatar

$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))
    }
}

Reply with quote

Advertisement

You can post new topics in this forum