Jump to content
HTRN

Script to Check for File and Exit if not found after a time

Recommended Posts

I have a script that opens a web browser,  enters in an URL and thru API pulls down a CSV file.  This is the first step in my script and once the file is downloaded I move it from downloads to another directory and do some other processing.   

If this file doesn't download I want to skip that whole section and move on to the next API Download.  I can't offer a sample of my script, Its kinda proprietary but the jist is this.>>

Heres what I do now.

shellexecute("Chrome.exe" , "https://www.somedb.com/query?FILEXYX&datatype=csv

Sleep(4999)

IF FileExists("C:\Users\jsmith

\Downloads\XYZ.csv") then
;Clear Dropbox for Today's csv processing.
FileDelete ("\\dropbox\dropbox\backups\*.*")
 

HOUSEKEEPINGSTEPS

PROCESSING STEPS

ELSE

;close chrome
winactivate("Untitled - Google Chrome","")
winwaitactive("Untitled - Google Chrome","")
send ("!{f4}")
WinWaitClose("Untitled - Google Chrome","","")

ENDIF

(Rinse and repeat with a different filename in the shell execute. 

I need to get rid of the Sleep(4999) and put in some code that looks for the file for 5 seconds. If it appears at anytime between 01-5000 miliseconds, immediately drop to the regular code after the THEN and process normally,  or if it doesn't exist after 5 seconds,  drop me down to the ELSE/ENDIF 

 

 

Share this post


Link to post
Share on other sites

You could replace the blind ShellExecute (...) by an intelligent InetGet ( "URL", "filename", $options , $background), which gives a lot of information if used in background along with its good friend InetGetInfo ($handle).  Take a look at those statements, I think you will like them...

Share this post


Link to post
Share on other sites
Posted (edited)

@HTRN, welcome to AutoIt and to the forum.

regarding simple file download, the suggestion above made by @Nine is good. for a more general-purpose approach, you can replace the Sleep() and the FileExists() condition with a call to a function like this:

Func _FileExists_Timeout($sFile, $iTimeout) ; $iTimeout in milliseconds
    ; start counting time
    Local $hTimer = TimerInit()

    ; do stuff as long as the time count has not yet reached timout
    While TimerDiff($hTimer) < $iTimeout
        If FileExists($sFile) Then Return True ; check if the file exists
        Sleep(100) ; do not hog the CPU
    WEnd

    ; if you reached this point, it means the file does not exist after timeout
    Return False
EndFunc   ;==>_FileExists_Timeout

read it carefully, look at the help file for TimerInit() and TimerDiff(). i have commented each line for you, but the code itself is quite simple. that function can be altered quite simply to match any process you need to check with time constraints.

Edited by orbs

Share this post


Link to post
Share on other sites

Instead of just checking if the file exists, I would suggest to use the InetGetInfo () options $INET_DOWNLOADREAD / $INET_DOWNLOADSIZE to create a progress bar.  That would make a high-quality UI especially for long downloads...

Share this post


Link to post
Share on other sites

Thank you both.  I will let you know how it shakes out..

 

Share this post


Link to post
Share on other sites
Posted (edited)

I appreciate the in depth information I get from  InetGet ( "URL", "filename", $options , $background) but for this application which is run after hours on a PC as stand alone script,  Orb's basic outline helped me build exactly what I needed.

I simply plugged in the static data for the variables,  plugged the returned "True" or "False"  into my IF THEN ELSE statement and it works like a charm.  

 

Edited by HTRN

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...