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?
When is a text file ready to read?
- WarlordAkamu67
- Posts: 232
- Joined: 21 Mar 2023, 06:52
Re: When is a text file ready to read?
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.
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
Re: When is a text file ready to read?
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)
}
}
}
Re: When is a text file ready to read?
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?
@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?
Re: When is a text file ready to read?
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.
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.
Re: When is a text file ready to read?
If you instead use the 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
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
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
Who is online
Users browsing this forum: mikeyww and 40 guests