The script below creates a child process to perform FileExist() separately from the main script so that it avoids halting the script when an unknown network path is passed.
However, the command, Process close, does not close the given process despite its ErrorLevel indicates it succeeded to close. Task Manager shows the child process still exists.
FileExistTimeout() ; this checks if the necessary parameters are passed
; this checks if the given path exists by creating a child process with the given timeout
PathToCheck := "\\1.2.3.4\test"
msgbox, 64, Test Result, % FileExistTimeout(PathToCheck) "`n" ErrorLevel
FileExistTimeout(strPath="", nTimeout=1000, strSwitch="/fe") {
global
strParam1 = %1%
strParam2 = %2%
; if the function is used to check the script parameters
if (strPath="") {
if (strParam2 = strSwitch) && strParam1 ; this means the function is placed at the beginning of the script.
ExitApp % (FileExist(strParam1) != "") + 1 ; Return 1 or 2.
Return
}
; create a child process to check the path
tooltip, RunWait is performing....
SetTimer, FileExistTimeout, % -1 * nTimeout
RunWait "%A_AhkPath%" "%A_ScriptFullPath%" "%strPath%" "%strSwitch%",,, nPID
SetTimer, FileExistTimeout, Off ; Disable the timer (if it hasn't fired already).
tooltip
; ErrorLevel contains the exit code of the process (from RunWait).
if ErrorLevel = 2 ; found
return true
else if ErrorLevel = 1 ; not found
return
return ; timeout
FileExistTimeout:
Process Close, %nPID% ; Timed out - terminate the process.
tooltip, % "Process Closed: " ErrorLevel "`n" "Used Pid: " nPID,,, 2
return
}