Jump to content
Jdr

More reliable check for file

Recommended Posts

Jdr

I'm trying to check for the existance of a file, on logon the file is there, then gets deleted and after a while recreated. I'm using this code:

Sleep(100)
        If not FileExists($sFile) Then
            Sleep(150)
               If FileExists($sFile) Then
                    GUICtrlSendToDummy($iDummy)

What happens is this: 

  1. User logs on, the file is present, or at least it usually is
  2. After about 10 seconds the file gets deleted
  3. Then after about 50 seconds the file gets created again, this is when I need the check, because now the process is finished.

The above code is part of a pretty long do....until false loop running an animation.

The problem is that sometimes this code fails. Without the sleep statements it didn't work at all so I think this is a timing issue. Pretty primitive code I admit, there must be a better way...

 

Edited by Jdr
typo

Share this post


Link to post
Share on other sites
orbs

if i get this right, then you need to play some animation while your script is waiting for the file to re-appear?

well, you can delegate the animation to an external process, and make your waiting loop a lot simpler:

; create the animation
While Not FileExists($sFile)
    Sleep(100)
    ; check for user clicking a cencellation button
WEnd

 

Edited by orbs

Share this post


Link to post
Share on other sites
Jdr
1 hour ago, Jos said:

So you currently wait 100Ms (0.10 second) initially, then test for the file and then 150Msecs later you test it again?

Jos

Yes, I admit that's pretty fast, basically it's constantly checking for the file

Share this post


Link to post
Share on other sites
Jdr
1 hour ago, Jos said:

So you currently wait 100Ms (0.10 second) initially, then test for the file and then 150Msecs later you test it again?

Jos

Yes, I admit that's pretty fast, basically it's constantly checking for the file

Share this post


Link to post
Share on other sites
Jdr
45 minutes ago, orbs said:

if i get this right, then you need to play some animation while your script is waiting for the file to re-appear?

well, you can delegate the animation to an external process, and make your waiting loop a lot simpler:

; create the animation
While Not FileExists($sFile)
    Sleep(100)
    ; check for user clicking a cencellation button
WEnd

 

Yes, that's what happens. Won't  "While Not" miss the beginning when the file is there? How can I get around that?

Share this post


Link to post
Share on other sites
orbs
24 minutes ago, Jdr said:

Won't  "While Not" miss the beginning when the file is there?

the real problem is that you have a single indication (the existence of a file) for two scenarios: before the process begins, and after the process ends. you better come up with a more precise distinction. you can, of course, do this:

; create the animation

; wait for first instance of the file to disappear
While FileExists($sFile)
    Sleep(100)
WEnd

; ok, the first instance disappeared. now wait for the second instance to appear
While Not FileExists($sFile)
    Sleep(100)
    ; check for user clicking a cencellation button
WEnd

but, you must be absolutely sure that your script runs before the second instance appears! if you miss that, your script will hang at the 1st loop, and never reach to an end.

Edited by orbs

Share this post


Link to post
Share on other sites
aleph01

If, per Orbs, you have to be sure your script runs, you may be able to use ProcessExists in a couple of loops to wait for your process to start then stop, one to wait for the script to start running, one to wait for the script to finish.


Meds.  They're not just for breakfast anymore. :'(

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

  • Similar Content

    • VADemon
      By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
    • Trisha
      By Trisha
      Hello,
      I Have clicked on save as option to save a file, while doing that I need to rename a file appending with sysdate. I have searched in google find the below one line of code:
      FileMove("C:\somefile.txt", "C:\somefile1.txt"), When I am trying to append with sysdate. It is not happening. Please help me out  with the small issue.
    • nacerbaaziz
      By nacerbaaziz
      Detect if the focus item is a button or check box
      Hello everyone.
      My dears I have a problem
      When I try to use
      ControlGetFocus function
      it  doesn't recognize the check  boxes.
      The result is that the selector is a button
      Although the specified is a check box
      but I want a way to identify the check boxes.
      Because I want to make a tool that helps screen readers users
      I hope you have the solution.
      Thank you in advance.
       
    • lewisg
      By lewisg
      Suddenly a RunWait command has stop working after 2 years of no errors, issues, or problems. The code uses RunWait to start Plink.exe, a command-line remote connection tool similar to UNIX ssh. I'm using it to ssh to a linux (Centos) machine, run a Perl script, and redirect the output to a file on a PC running the AutoIt script.  
       
      $FilePath = "C:\AutoIT\LED" $FilePathPlus = $FilePath & "\plink.exe" $Code1 = RunWait(@ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ?????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt ", @SW_SHOW) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : @ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt " = ' & @ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ?????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt " & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console The ConsoleWrite output when cut-n-pasted into a DOS box produces the expected file so I know it works. I've also tried many variations of the function moving, adding, and changing the " and ' (quote) marks. Also tried it without the @ComSpec macro and other related functions...e.g.  ShellExecuteWait , etc.
      The PC is a Windows 10 64bit and AutoIT is version 3.3.14.2.
      Searching here and Google has not yielded any clues that helped. 
    • rudi
      By rudi
      Hello.
      I'm too stupid to see my mistake:
      To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.
      Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting
      The "TIFF Header Format" is easy:
      Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)
      --> II
      Offset 02h, 2 Byte = Version Nr.
      Offset 04h, 4 Byte = pointer to first IFD entry
      Description of TIFF header: https://www.awaresystems.be/imaging/tiff/faq.html#q3
       

      Howto read and analyse the binary content correctly? This is my messy, not operational code:
       
      $sampleTiff="H:\daten\tif\11\11\111111.TIF" $h=FileOpen($sampleTiff,16) $content=FileRead($h) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console FileClose($h) $type=VarGetType($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $ToString=BinaryToString($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToString = ' & $ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & @CRLF) $content=StringTrimLeft($content,2) ; cut off the leading "0x" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console for $i = 1 to 8 step 8 $next=StringMid($content,$i,2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $next = ' & $next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $Chr=BinaryToString($next) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Chr = ' & $Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & "---" & @CRLF) Next Regards, Rudi.
      111111.TIF
×