AutoHotkey Community

It is currently May 27th, 2012, 6:49 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: FileExist() Timeout
PostPosted: September 4th, 2011, 10:00 am 
Offline

Joined: August 29th, 2011, 10:35 pm
Posts: 30
Location: Tokyo
Hi,

How can I set a timeout for FileExist()? The code below takes 45 seconds to return False. Im my actial script, there are lots of files that are automatically generated including files on network drives and it takes too much time.
Code:
if FileExist("\\199.199.199.199\arbitrary location")
   msgbox True
Else
   msgbox False


[ Moderator!: Moved from Ask for Help ]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2011, 10:07 am 
Offline

Joined: February 1st, 2010, 2:50 pm
Posts: 237
Location: Netherlands
Perhaps you could Ping the address first to see if there is a device with that address on the network.

_________________
I'm just trying to help, so don't kill me if i'm wrong.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 6:00 am 
Offline

Joined: August 29th, 2011, 10:35 pm
Posts: 30
Location: Tokyo
I can think of two reasons that I don't like to use Ping as a workaround.

1. Some security software catch the script trying to ping and the user may see it as suspicious activity.
2. Even the device is connected, Ping has to be sent, which will be slow if all checking devices are connected.

Moderators, please move this topic to Wish List. I'd like FileExist() to handle time-outs. Thanks.

_________________
English teachers are welcome.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 9:53 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
FileExist uses FindFirstFile if wildcards are used or GetFileAttributes otherwise. Neither function provides any way to set a timeout, as far as I can tell.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 12:29 pm 
Can't we do something like this, wrapping it in a class with auto-release functionality?
Code:
cFE := new AutoRelease
if cFE.FileExist("\\192.199.199.199\some folder")
   msgbox True
else
   msgbox False
Return

class AutoRelease {
    FileExist(path, timeout=1000) {
      SetTimer, FileExistTimeout, % timeout
        if FileExist(path)
         Return True
      Else
         Return False
    }
   Labels() {
      global
      FileExistTimeout:
         cFE := ""      ;release the self instance
      Return   
   }
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 2:06 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
No. The timer message won't be processed until FileExist returns.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 6:25 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
You can do it using AutoHotkey.dll, e.g.:
Code:
MyFile:= "\\192.168.2.2\MyData\MyFile"

MsgBox % FileExistTimeout(MyFile,3000)
ExitApp

#include <AhkDllObject>
FileExistTimeout(file,TimeOut=1000){ ; TimeOut in Msec
   static dll:=AhkDllObject("AutoHotkeyMini.dll")
   dll.ahktextdll("#Persistent`nFilePattern:=FileExist(""" file """)`nsuccess:=1")
   Start:=A_TickCount
   While % (A_TickCount-start<TimeOut && !dll.ahkgetvar.success)
      Sleep 10
   If (dll.ahkgetvar.success){
      success:=dll.ahkgetvar.FilePattern
      dll.ahkterminate()
      return success
   } else
      return dll.ahkterminate() ;returns 0 to caller = TimeOut
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 6th, 2011, 1:45 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
If you're looking for workarounds, you can just use a separate script (or the same script in this case):
Code:
if 0 > 0 ; Command-line arg was specified.
{
   Menu Tray, NoIcon

   DllCall("Sleep", "int", 5000) ; Simulate long delay.
   ExitApp % (FileExist(%true%) != "") + 1 ; Return 1 or 2.
}
   
PathToCheck := "C:\Windows"
SetTimer timeout, -500 ; Set a timeout of 500ms.
; Run the script (could be a separate script) with the path to check
; as a command-line arg:
RunWait "%A_AhkPath%" "%A_ScriptFullPath%" "%PathToCheck%",,, pid
SetTimer timeout, Off ; Disable the timer (if it hasn't fired already).
; ErrorLevel contains the exit code of the process (from RunWait).
if ErrorLevel = 0
   MsgBox Timed out.
else if ErrorLevel = 1
   MsgBox File does not exist.
else
   MsgBox File exists.
ExitApp


timeout:
   ; Timed out - terminate the process.
   Process Close, %pid%
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2011, 11:34 am 
Offline

Joined: August 29th, 2011, 10:35 pm
Posts: 30
Location: Tokyo
@HotKeyIt, thanks but AutoHotkey.dll does not seem to be fully compatible with the 64 bit build.

@Lexikos, thanks but it always returns "Timed out." What is the line, DllCall("Sleep", "int", 5000) for? If I decrease the value to 50 from 5000, then the script started saying "File exists."

_________________
English teachers are welcome.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2011, 2:13 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
A_Samurai wrote:
@Lexikos, thanks but it always returns "Timed out." What is the line, DllCall("Sleep", "int", 5000) for?
Read the comments. That line is to "simulate long delay." In other words, cause a timeout. :roll:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2011, 2:46 pm 
I see. So the line should be removed in an actual script.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group