Page 1 of 1

Can I use EXISTS using jokers?

Posted: Fri Jan 22, 2021 10:39 am
by Bugala
I am having here a situation where I want to check if certain graphic file exists or not. However, I don't want to know in what format that GFX is in.

therefore, is there a way to do something like:

Code: Select all

if exists("filename."..wildcard)
Matchpattern has this * to be used as a wildcard, but is there one for Exists()?

Re: Can I use EXISTS using jokers?

Posted: Sun Jan 24, 2021 12:16 pm
by msu
I would solve it like this:

Code: Select all

formats = {".gif", ".png", ".jpg"}

file$ = "FILE"

For a = 0 to TableItems(formats) -1
	If Exists(file$ .. formats[a])
		Print("File exists!")
		WaitLeftMouse
		End
	EndIf
Next

Print("File does not exist!")
WaitLeftMouse
End

Re: Can I use EXISTS using jokers?

Posted: Mon Jan 25, 2021 7:35 pm
by jPV
I'm not sure if wildcards are practical with an Exists function... don't you still want to know exact files?

But anyway, here's my take :)

Code: Select all

; An "Exists" function which works with (MatchPattern) wildcards, and is case insensitive.
Function p_Exists(f$)
    Local d$ = PathPart(f$)
    f$ = LowerStr(FilePart(f$))
    For s$,t In DirectoryItems(d$)
        If MatchPattern(LowerStr(s$), f$) Then Return(True)
    Next
EndFunction

; Some examples:
If p_Exists("ram:env") Then DebugPrint("ram:env exists!") Else DebugPrint("doesn't exist")
If p_Exists("ram:clip*") Then DebugPrint("ram:clip* exists!") Else DebugPrint("doesn't exist")
If p_Exists("ram:*boards") Then DebugPrint("ram:*boards exists!") Else DebugPrint("doesn't exist")
If p_Exists("ram:test*file") Then DebugPrint("ram:test*file exists!") Else DebugPrint("doesn't exist")
If p_Exists("*.hws") Then DebugPrint("*.hws exists!") Else DebugPrint("doesn't exist")

Re: Can I use EXISTS using jokers?

Posted: Tue Jan 26, 2021 7:42 am
by Bugala
@JPV Thanks, was thinking a solution something like that myself too, just didn't realize it would be so easy to do actually until saw how your code took only a couple of lines.

The point about not needing to know the file format was two different ideas. First one was that I was basically hoping an exists function that would use wildcard, but yet return the full name of the found file.

Another possibility was that I was thinking of this MODding option in which case that part of code wouldn't need to know the full file name, but only if there is such a file existing with any suffix. Idea being that it was loading button graphics and then depending if there are for example highlight graphics existing for that button, would depend if button would have highlight, and that specific function that was making the button didn't need to know the filename yet, it only needed to know if such thing existed or not.

@msu

Thanks for your code too. Was thinking that one as an option too, but decided to abandon it since I wanted it to support any gfx format that Hollywood supports, in the future too, and I didn't want to start maintaining/updating that list in the future but wanted a solution that would work by simply compiling with a future Hollywood version.