When is a text file ready to read?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

When is a text file ready to read?

Post by Albireo » 23 May 2024, 10:30

Suppose there is another program that creates a readable txt file.
Autohotkey is waiting to read the created file.
(some files may take 1 second to create while others take 30-40 seconds (or more) to create.)

Is there a good way to know when the text file is ready to read?

User avatar
WarlordAkamu67
Posts: 232
Joined: 21 Mar 2023, 06:52

Re: When is a text file ready to read?

Post by WarlordAkamu67 » 23 May 2024, 11:09

Hello. This has worked for me, but maybe somebody has a better/more optimal solution.

I have AHK that creates text files, then runs JavaScript that reads those files, which in turn creates other text files that AHK reads. I have not run into any issues with it. The spoiler contains some of my code.
Spoiler

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: When is a text file ready to read?

Post by mikeyww » 23 May 2024, 11:33

Mine is below.

Code: Select all

#Requires AutoHotkey v2.0
fPath := 'test.txt'
txt   := read(fPath)
MsgBox txt, 'Text', 'Iconi'

read(fPath) {
 Loop {
  Try FileRead(fPath)
  Catch {
   SoundBeep 1500
   Sleep 900
  } Else {
   Sleep 1500
   Return FileRead(fPath)
  }
 }
}

Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Re: When is a text file ready to read?

Post by Albireo » 23 May 2024, 19:15

Thanks! (both of you)
@mikeyww:s suggestions are easier to understand. ;)
But, I don't see what parses if the text file is ready to read.
Is the file locked for reading while it is being created? (I'm not sure)
I don't know how the file is created by the other program.
Suppose I'm going to create a CSV file with a loop and fileAppend.
Isn't the text-file locked for a very short time?

Will experiment a bit..
I'm thinking a bit about eof (end of file)
When is eof created in a text file?
Or is the filesize better to analyse? (When did the CSV file stop growing in size?)

_ - - - - _ - - - - -_ - - - - _ _ - - - - _ _ - - - - _ _ - - - - _ _ - - - - _

Can describe some cases where this occurs.
1)
In a PDA, a database is created by scanning barcodes. (I don't know how to read the database with AHK directly in the PDA)
When the PDA is placed in a docking station, AHK can start a communication program,
that transfers data from the database to a text file (CSV format) on the computer (Windows).
The content must then be analyzed by AHK.
When can AHK analyze the file? (want to start analyzing as soon as possible)

2)
AHK launches another program that creates a CSV file.
1) When is the file available?
2) When is the export of the CSV file finished?

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: When is a text file ready to read?

Post by mikeyww » 23 May 2024, 19:50

Try it and see. You will be the person who decides upon the definition of a "completed" file. Various definitions could be crafted.

Most likely, a text file can be read when it exists, but you can try it to confirm. You are able to answer your question by running your script.

TAC109
Posts: 1129
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: When is a text file ready to read?

Post by TAC109 » 23 May 2024, 20:04

If you instead use the :arrow: FileOpen object to read your file you can specify the appropriate sharing mode to ensure that the file has been closed by other processes.

Cheers
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe

Post Reply

Return to “Ask for Help (v2)”