How long does session.GetFiles() hang on to file?

Advertisement

lkubler
Joined:
Posts:
8
Location:
Wisconsin

How long does session.GetFiles() hang on to file?

Hi,

Forgive my long-windedness here...

The last operation of my script checks the SFTP site for any files to download. Then downloads them to a local folder. Every 15 minutes another program looks in the local folder for any new files, reads them and processes their contents updating a database. Then it moves the files to another folder to keep the download folder empty.

What happened during the last test is that the database update appears to have happened twice and I did notice from visual observation that the downloaded file didn't get moved for over 20 - 25 minutes. So I'm guessing it read the file, failed to move it and then next time around read the file again and then moved it successfully. Hope that made sense.

I don't have control or access to this other process so I'm having the vendor look into it. In the mean time I'm doing my own investigation and one possiblility I thought of could be that if the file is being held by another process it might have appeared locked and couldn't be deleted from the download location.

Which begs the question, how long does the session hang on to the files after a GetFiles() method is run? Here is the relevent code from my script.

      # No new orders to send or finished sending.
      # Check remote (MSD) site for return files.
      $directory = $session.ListDirectory($remotePath + "/Outgoing")
      
      # Select files matching wildcard.
      $remoteFiles = $directory.Files | Where-Object { $_.Name -like "*.csv" }
      
      if ($remoteFiles) 
      {
      # Download each return file.
         foreach ($file in $remoteFiles)
         {
            $transferResult = $session.GetFiles(("$remotePath/Outgoing/$file"), 
               ("$localReturn\*.*"))
            
            $transferResult.Check()
            
            foreach ($transfer in $transferResult.Transfers)
            {
               $fileOnly = $transfer.Destination.Substring($localReturn.Length + 1)
               Write-Host ("Source: {0}`nDestination: {1}" -f 
                  $transfer.FileName, 
                  "$remotePath/Outgoing/Archive/$file")
               $session.MoveFile(($transfer.FileName), 
                  "$remotePath/Outgoing/Archive/$file")
            }
         }
      }
   }
   finally
   {
      # Disconnect, clean up
      $session.Dispose()
   }
   Write-Host "Done!"
   exit 0
}
catch [Exception]
{
   Write-Host $_.Exception.Message
   Write-Host 
   Write-Host "Complete with Error!"
   exit 1
}

Thanks in advance,
Linn

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Re: How long does session.GetFiles() hang on to file?

If you are asking whether WinSCP keeps the downloaded files locked after the GetFiles finishes, it does not.

Reply with quote

Advertisement

You can post new topics in this forum