Download a file from a website

When you automate a file download from a website, you need to wait until the Download complete window before processing the next step. The script below gives an example of such a file download.

You can find the corresponding script file under Script_Examples, sub-directory of scripts.

 

'Internet Explorer must be configured for a window telling that the download is finished.

 

'Specify the maximum time allowed for the download to complete

Tmax_Download=300

 

'Specify the filename for the downloaded file, it must include the full path.

file_name$="c:\wttest\test.exe"

 

'launch the url which opens the File download dialog box
'As the File Download window prevents the IE page to be fully loaded before clicking a button in this small window,
'the Shell function must be used instead of StartBrowser function.
'Under a x64 Windows, call iexplore.exe which is in Program Files (x32) (the x32 version of IE).

 

Shell(Chr$(34)+"C:\Program Files\Internet Explorer\iexplore.exe"+Chr$(34)+" www.wintask.com/wintaskdemo.exe",1)

'We wait until the File download window is displayed

Pause 5 secs until

WinStatus(Active)

InWindow("IEXPLORE.EXE|#32770|File Download",1)

PauseFalse

MsgBox("'Wait for' at line " + #ErrorLine$ + " has failed !",16,"Runtime error")

End

EndPause

 

'We click Save button

UseWindow("IEXPLORE.EXE|#32770|File Download - Security Warning",1)

Click(Button,"&Save")

'If the file already exists, we delete it.
if exist(file_name$)=1 then
kill(file_name$)
endif

 

 

'We type the filename in the Save As dialog box and press Enter button to save.
'Under Vista, the Save as window name is as below:
UseWindow("IEUSER.EXE|#32770|Save As")

'Under XP, the Save as window name is as below:

'UseWindow("IEXPLORE.EXE|#32770|Save As",1)

SendKeys(file_name$,Noactivate)
SendKeys("<Enter>")

 

'When Download complete window complete is there, the download has ended and we can click Close button

Pause Tmax_Download until

WinStatus(Active, Exact)

InWindow("IEXPLORE.EXE|#32770|Download complete",1)

PauseFalse

MsgBox("'Wait for' at line " + #ErrorLine$ + " has failed !",16,"Runtime error")

End

EndPause

 

UseWindow("IEXPLORE.EXE|#32770|Download complete",1)

Click(Button,"Close")