Wait a file exist

Find quick help here to get you started with Hollywood
Post Reply
papiosaur
Posts: 161
Joined: Fri Mar 31, 2023 1:34 pm

Wait a file exist

Post 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!
Flinx
Posts: 192
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Wait a file exist

Post 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

User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Wait a file exist

Post 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
papiosaur
Posts: 161
Joined: Fri Mar 31, 2023 1:34 pm

Re: Wait a file exist

Post by papiosaur »

Thanks lot guys !!!

I will try them.
papiosaur
Posts: 161
Joined: Fri Mar 31, 2023 1:34 pm

Re: Wait a file exist

Post 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!
User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Wait a file exist

Post 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.
papiosaur
Posts: 161
Joined: Fri Mar 31, 2023 1:34 pm

Re: Wait a file exist

Post 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.
papiosaur
Posts: 161
Joined: Fri Mar 31, 2023 1:34 pm

Re: Wait a file exist

Post by papiosaur »

Ok, seems ok now, i realize the test of the outputfile before FileToString() command
Post Reply