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

Re: Problem with moving files from remote folder to local folder

Your logic seems strange. Why are you downloading the files twice? That's a waste of time, bandwidth and your server resources.

Anyway, what is definitely wrong, is that you trigger the second download of all files for each previously downloaded file. Do it only once. Most of your code that is within foreach ($transfer in $transferResult.Transfers) should be outside of it.

Though, that does not explain, why no file is moved. If you need a help with that, we need a session log file.

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
rbylnz

Problem with moving files from remote folder to local folder

Hello everyone!
I am creating a script that will help me do the following:
1. Copy files from remote folder A to local folder B.
2. If transfer (step 1) is successful, move files in remote folder A to local folder C. (archive folder)
If transfer (step 1) is unsuccessful, move files in remote folder A to local folder D. (error folder)

I do not have any problems copying the files (step 1). I am having trouble performing the move operation. I am trying to use the Session.GetFiles method within a loop, as you can see in the code below. I get no errors, but I see that nothing is moved. I would appreciate any help or suggestion.

$period = [TimeSpan]::FromSeconds(20)
$lastRunTime = [DateTime]::MinValue
while (1)
{
    while ((Get-Date) - $lastRunTime -lt $period) {
        Start-Sleep -Milliseconds 5000
        Write-Host "Waiting for new files..."
    }
 
    $lastRunTime = Get-Date
 
    try
    {
        # Load WinSCP .NET assembly
        Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
     
        # Setup session options
        $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
            Protocol = [WinSCP.Protocol]::Sftp
            HostName = "BLABLABLA"
            UserName = "BLABLABLA"
            Password = "BLABLABLA"
            GiveUpSecurityAndAcceptAnySshHostKey = "true"
        }
     
        $session = New-Object WinSCP.Session
     
        try
        {     
            # Connect
            $session.Open($sessionOptions)
     
            # Set transfer options
            $remotePath = "/usr/BLABLA/REMOTEPATH/"
            $transferOptions = New-Object WinSCP.TransferOptions
            $transferOptions.TransferMode = [WinSCP.TransferMode]::Ascii
 
            $transferResult = $session.GetFiles("/usr/BLABLA/REMOTE PATH/*.txt",
                "C:\Users\BLABLA\LOCALFOLDER-A\", $False, $transferOptions)
         
            $transferResult.Check()
 
            foreach ($transfer in $transferResult.Transfers)
            {
                if ($transferResult.IsSuccess)
                {
                    # Download success message
                    Write-Host ("File {0} was downloaded successfully..." -f $transfer.FileName.TrimStart("/usr/BLABLA/REMOTE PATH/"))
                    $FileName = $transfer.FileName.TrimStart("/usr/BLABLA/REMOTE PATH/")
                    $A = (get-date (get-date) -UFormat "%Y-%m%-%d %H:%M")
                    Add-Content C:\Users\BLABLA\LOCALFOLDER-A\Log\LogFile.log "$FileName | $A | Downloaded"
                    #MOVE OPERATION IS HERE: (REMOTE TO LOCAL-B (ARCHIVE FOLDER))
                   
                    $transferResult2 = $session.GetFiles("/usr/BLABLA/REMOTE PATH/*.txt",
                       "C:\Users\BLABLA\LOCALFOLDER-B\", $True, $transferOptions)
                   
                }
                elseif (!$transferResult.IsSuccess)
                {
                    #MOVE OPERATION IS HERE: (REMOTE TO LOCAL-C (ERROR FOLDER))
               
                    $transferResult3 = $session.GetFiles("/usr/BLABLA/REMOTE PATH/*.txt",
                        "C:\Users\BLABLA\LOCALFOLDER-C", $True, $transferOptions)
                    # Download error message
                    Write-Host ("Error downloading file {0}!" -f $transfer.FileName, $transferResult.Failures[0].Message)
                    $B = (get-date (get-date) -UFormat "%Y-%m%-%d %H:%M")
                    Add-Content C:\Users\BLABLA\LOCALFOLDER-A\Log\LogFile.log "$FileName | $A | Download error", $transferResult.Failures[0].Message
                }
            }
        }
        finally
        {
            # Disconnect, clean up
            $session.Dispose()
        }
 
    }
 
    catch [Exception]
    {
        Write-Host ("Error: {0}" -f $_.Exception.Message)
    }
    Start-Sleep -s 10
}