Jump to content

More reliable check for file


Jdr
 Share

Recommended Posts

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

  • Developers

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

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

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. :'(

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...