Jump to content

_FileExistsTimeout()


Gibbo
 Share

Recommended Posts

Hi All, 
I went searching for something to speed up checking for files on the network (UNC Paths)

Most examples were rather complicated but someones multi-threaded solution gave me an idea.

Hope it is useful to someone.

Seems to work well so far. 
 

Func _FileExistsTimeout($sPath, $iTimeout = 1000)
    Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
    Local $iPID = Run(@AutoItExe & ' /AutoIt3ExecuteLine  "ConsoleWriteError(FileExists(""' & $sPath & '""))"', @ScriptDir, @SW_HIDE, $STDERR_CHILD)
    Local $sOutput = ""
    Local $iDiff = 0
    While $iDiff < $iTimeout
        $iDiff = TimerDiff($hTimer)
        $sOutput &= StderrRead($iPID)
        If @error Then ; Exit the loop if the process closes or StderrRead returns an error.
            ExitLoop
        EndIf
    WEnd
    ProcessClose($iPID)
    Switch StringStripWS($sOutput, 8)
        Case 1
            Return True
        Case Else
            Return False
    EndSwitch
EndFunc   ;==>_FileExistsTimeout
Link to comment
Share on other sites

  • 9 years later...

This seems to be a well thought out great function. Solved my issue for sure.  Just don't spend hours like me trying figure out why it doesn't work.

The author didn't leave a clue about needing the following, you know, because we are already supposed to know this. Right... 

See https://www.autoitscript.com/autoit3/docs/functions/StderrRead.htm

pragma compile(AutoItExecuteAllowed, True)
#include <AutoItConstants.au3>
 

Link to comment
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
 Share

×
×
  • Create New...