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

Author Message
[View user's profile]

Joined: 2015-02-18
Posts: 7
Location: Wisconsin

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"),
            foreach ($transfer in $transferResult.Transfers)
               $fileOnly = $transfer.Destination.Substring($localReturn.Length + 1)
               Write-Host ("Source: {0}`nDestination: {1}" -f
      # Disconnect, clean up
   Write-Host "Done!"
   exit 0
catch [Exception]
   Write-Host $_.Exception.Message
   Write-Host "Complete with Error!"
   exit 1

Thanks in advance,
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26890
Location: Prague, Czechia
If you are asking whether WinSCP keeps the downloaded files locked after the GetFiles finishes, it does not.
Martin Prikryl

You can post new topics in this forum


What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License