-1

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?

Dominique
  • 16,450
  • 15
  • 56
  • 112
R.Dave
  • 1
  • 4
  • The issue you are getting is because of the specific file's attribute and not related to your code.. You can create some dummy txt file and it will work properly. I did the testing in my local and it is not having any issue except the timeout is high in my case. – Ranadip Dutta Apr 24 '19 at 11:14
  • sorry but i dont understand what you mean by the specific file attributes. – R.Dave Apr 24 '19 at 11:22
  • 1
    See the error you are getting is because of one file and not related to the code. You just take sample 5 txt files and try removing them, your script will work like a charm. – Ranadip Dutta Apr 24 '19 at 11:23

2 Answers2

1

Can't get attributes of file

The error/log file for sure includes more details, that will tell us the root cause.

But most probably it means that the file simply does not exist.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

I agree with Martin Prikryl that the file you are after probably does not exist. You could build in a check to find out if the file exists or not using something like this:

if ($session.FileExists($fileToRemove)) {
    $session.RemoveFiles($fileToRemove).Check()
    Write-Host "File '$fileToRemove' removed."
    # exit the while loop, because the action succeeded
    break
}
else {
    Write-Host "File '$fileToRemove' not found."
}

Also, I would close the session after the while loop using $session.Close. Maybe you already do that, but it does not show in your code.


Putting it together with your 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"

        if ($session.FileExists($fileToRemove)) {
            # we know now that the file actually exists, so the next line should not throw the exception
            $session.RemoveFiles($fileToRemove)
            Write-Output "File '$fileToRemove' removed."
            # exit the while loop, because the action succeeded
            break
        }
        else {
            Write-Output "File '$fileToRemove' not found."
        }

    }
    catch {
        # some other exception happened
        echo ("File not removed retrying for the " + $retryTimes + " time.")
        echo ($Error[0].Exception)
    }
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • where would i put this code into mine? after the catch statement? – R.Dave Apr 24 '19 at 13:21
  • @R.Dave No, it should go inside the `try{..}` block, directly above the `catch{..}`. It replaces these two lines: `$session.RemoveFiles($fileToRemove)` and `echo ("File" + $fileToRemove + "removed." )` – Theo Apr 24 '19 at 13:24
  • @R.Dave P.S. I have added a small piece to exit the while loop if the removal succeeded. – Theo Apr 24 '19 at 13:27
  • i added you code with mine and it is not working. i think i am not placing it in the right place. could you edit your code and add my code with yours please. it will help me understand where i am going wrong. thanks! – R.Dave Apr 24 '19 at 14:35
  • @R.Dave Sorry for the really late reply.. I have put the snippet in your code and added it to my answer. – Theo Apr 27 '19 at 14:15