I have a powershell script that connects to a SFTP every 2 minutes and deletes the files. If the file does not get deleted it retries 10 times.
code:
$session = New-Object WinSCP.Session
$retryTimes = 0
while ($retryTimes -ne 10) {
try {
$retryTimes++
$session.Open($sessionOptions)
echo ("Opened a session with options: " + $sessionOptions)
echo ("Trying to remove a file: " + $fileToRemove)
$fileToRemove = "/File_$((Get-Date).AddDays(-1).ToString("yyyyMMdd_HHmm")).csv"
$session.RemoveFiles($fileToRemove)
echo ("File" + $fileToRemove + "removed." )
}
catch {
echo ("File not removed retrying for the " + $retryTimes + "time.")
echo ($Error[0].Exception)
}
}
I have noticed that it doesn't delete files when it runs every 2 mins sometimes and other times it deletes fine.
I checked my logs and I am getting an error of - {} {WinSCP.SessionRemoteException: Can't get attributes of file
not sure what this means.
Is there a way to make sure it deletes the file?