How to know when download is finished

I am using uipath to download images from the web.

Once I download them, I use this function to get the most recently downloaded file from my downloads folder:

String.Join("",Directory.GetFiles("C:\Users\Sami\Downloads","*",SearchOption.AllDirectories).OrderByDescending(Function(d)New FileInfo(d).CreationTime).Take(1))

Then I use the “move file” activity to move the file to a new folder.

However, most files take a while to download so how could I make it so it waits for it to download before moving the files? (Without a delay)

Could you use a waitForReady property?

int MaxRetries = 5
     Delay 3 seconds
     MaxRetries = MaxRetries - 1
While Not File.Exist("file") and MaxRetries >= 0
if(MaxRetries <= 0) then
Move File "file"

Check download location.

For i.e: Firefox when you start download file is created but size is 0 KB, so maybe you can check if size of file is larger than 0 and then move it. If still 0 wait and retry.

For IE: file with “partial” at the end is created, so you can check file extension Path.GetExtension Method, when extension changed to normal (any other/no longer “partial”) name it is downloaded and read to move.

1 Like

Hi buddy @Sami_Syed
Once the file is downloaded with a activity to the folder use a activity next to that called PATH EXISTS…
and mention the file path in this activity as input and we will be getting a boolean value as output b variable named out_boolean whether the file exists in that path or not…
– after this path exist activity use a if condition and mention this variable out_boolean in the condition like this

If this condition gets passed it denotes that your file exists and can continue further…in the THEN part of if condition
Or if it gives false it will go to ELSE part where you can mention these activities within else part of if condition in order

  1. Delay activity with some time set to it
  2. Then again use a if condition and pass the same boolean output variable and check…it pass (hopefully it should )…then we can continue now with the next step of activities…

This will work for sure
Kindly try this and let know whether this works or not buddy
Cheeers @Sami_Syed

1 Like

Did that work buddy @Sami_Syed

This logic does not work if the use case involves downloading and overwriting the existing file.

May I know what was the issue you were facing
Cheers @RajeshSwarnkar

There are two approaches I would suggest in this case.

  1. Check if the file already exists prior to downloading, so you can skip all those steps and perform the process quicker.
  2. Delete the file prior to downloading so it doesn’t already exist.

Also, I suggest using a Retry Scope rather than Do While. It has timeout built-in so you don’t need so much logic with it. Just rename the Retry Scope to something like “Retry - Wait for File” and use a Path Exist activity or Is True with condition File.Exists(path) in the condition section. And, set the delay to 1 second with a retry number of how many seconds you would like to wait for the file for. If the file never downloads, it will throw an error showing this activity as the source, and perform a retry on it.