Page 1 of 1
Wait a file exist
Posted: Thu Dec 28, 2023 9:56 am
by papiosaur
Hello,
i would like realize an action on a file but i must wait it exist before realize the action.
Is there a possibility to do a loop about that?
Thanks a lot for your help!
Re: Wait a file exist
Posted: Thu Dec 28, 2023 12:33 pm
by Flinx
You have two options. A loop is simple.
Code: Select all
filename$="test.txt"
Repeat
Cls
If Exists(filename$)
TextOut(#CENTER, #CENTER, filename$.." exists")
Else
TextOut(#CENTER, #CENTER, filename$.." does not exist")
EndIf
Wait(100,#MILLISECONDS)
Forever
But a loop always requires computing time. An event would be the smarter solution.
In the following example the current directory is monitored (".").
Code: Select all
filename$="test.txt"
Function p_EventDirectoryChanged(msg)
Cls
TextOut(#CENTER, #CENTER, "id: "..ToString(msg.id).."\ndirectory: "..msg.directory.."\naction: "..msg.action.."\ntype: "..msg.type.."\nname: "..msg.name)
EndFunction
InstallEventHandler({DirectoryChanged=p_EventDirectoryChanged})
MonitorDirectory(1, ".",{ReportChanges=True})
Repeat
WaitEvent()
Forever
Re: Wait a file exist
Posted: Thu Dec 28, 2023 12:47 pm
by jPV
Unfortunately ReportChanges=True for the
MonitorDirectory() does only work on Windows and OS4, AFAIK. At least it fails on MorphOS, which papiosaur uses I guess.
Here are my two more options
If doing purely with Hollywood and don't want to use the looping solution, something like this with the
MonitorDirectory() (a bit clumsy example and would need finetuning depending on the final goal):
Code: Select all
Function p_Monitor(msg)
If Exists(msg.MonitorUserData)
Print("The file is here!")
; Do something with the file
; And then stop monitoring
CloseDirectory(g_monitorid)
g_monitorid = Nil
EndIf
EndFunction
Function p_MonitorFile(file$)
Local dir$ = PathPart(file$)
MakeDirectory(dir$)
InstallEventHandler({DirectoryChanged = p_Monitor})
g_monitorid = MonitorDirectory(Nil, dir$, {UserData=file$})
EndFunction
p_MonitorFile("Ram Disk:test/testfile")
Repeat
WaitEvent
Forever
Or if you are creating a program for MorphOS only and want to block the script execution while waiting a file to appear, you can use the
WaitForNotification shell command.
Code: Select all
file$ = "Ram Disk:test/testfile"
If Not Exists(file$)
Execute("WaitForNotification", "\"" .. file$ .. "\"")
EndIf
Re: Wait a file exist
Posted: Thu Dec 28, 2023 1:09 pm
by papiosaur
Thanks lot guys !!!
I will try them.
Re: Wait a file exist
Posted: Thu Dec 28, 2023 6:26 pm
by papiosaur
I use WaitForNotification command on MorphOS and it work but i will try the others solutions to understand better
What i must do when i use
FileToString() command, how to know when the conversion is finalized please?
Thanks!
Re: Wait a file exist
Posted: Thu Dec 28, 2023 6:42 pm
by jPV
papiosaur wrote: ↑Thu Dec 28, 2023 6:26 pm
What i must do when i use
FileToString() command, how to know when the conversion is finalized please?
No need to do anything, the script halts until a file is read in a string and continues to the next command when it's done.
Re: Wait a file exist
Posted: Thu Dec 28, 2023 6:56 pm
by papiosaur
@jPV: Sometimes the conversion is too long and the string is not showed in a log window asked just after.
Note: i compile a c source file with gcc via the GUI and after i want to execute the exe too
I would like to show the outfile in a log window and i must use
FileToString() command to do that.
I use
Wait() command but i don't like this solution.
Re: Wait a file exist
Posted: Thu Dec 28, 2023 11:03 pm
by papiosaur
Ok, seems ok now, i realize the test of the outputfile before
FileToString() command