$session.FileExists() always returns true
When using the
Here's my code, omitting session information:
FileExists
and GetFileInfo
methods, the return value is an empty file instead of an error. The same issue occurs when trying to get a file that doesn't exist: I receive a directory file with a size of 0 created at the time of the call.
Here's my code, omitting session information:
# Set the path to the file that you want to check for on the SFTP server $filename = "New"+(Get-Date).ToString("yyyyMMdd")+"*.txt" # $filename="file.txt" $filePath = "/directory/"+$filename # Set the local path where you want to download the file $localPath = "C:\Users\ristianchrathke\Downloads\" # Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Set up session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = $source_host UserName = $source_username Password = $source_password SshHostKeyFingerprint = "xxxxxxxxxxx" } $session = New-Object WinSCP.Session $session.Open($sessionOptions) echo "connection established" # Set up a loop to check for the file every 15 minutes while ($true) { # Check if the file exists on the SFTP server if ($session.FileExists($filePath)) { # Download the file to the specified local path $transferResult = $session.GetFiles($filePath, $localPath+"*") $transferResult.Check() echo "File downloaded" # Disconnect from the SFTP server $session.close() echo "session closed" # Break out of while loop break } echo "file not found, waiting 15 minutes before next filecheck..." # Wait for 15 minutes before checking again Start-Sleep -Seconds 900 } # Get yesterday's date $yesterday = (Get-Date).AddDays(-1).ToString("yyyyMMdd") # Rename the downloaded file to "BAI_" followed by yesterday's date Rename-Item "$localPath\$filename" "New$yesterday.txt"