Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
leeladhar
Visitor

How to check if a file exists in roku

Hi,

I am using SimplePoster.brs, How can we achieve below functionality.

From XML Feed, I get an image name, I want to check whether that file exists, if it doesn't exist then i need to show a default image

This is what i want to do, how to check the file_exists functionality in BrightScript ?

if(file_exists(currShow@hdImg))
Item.hdImg = validstr(currShow@hdImg)
else
Item.hdImg = "pkg:/images/default.png"
end if

For now when I try to use an non exisitng file name i am getting error, ***ERROR: Missing or invalid PHY
After searchng forums, i got to know that we can do error_handling using Eval(), but can some one explain how to use it in above scenario ?

Thanks
Leeladhar
0 Kudos
3 REPLIES 3
RokuChris
Roku Employee
Roku Employee

Re: How to check if a file exists in roku

To check for the existence of a local file, the roFileSystem component has an Exists() function.

If you want to check for the existence of a remote file, you could use the roURLTransfer component to perform a head request on the URL. Doing so for every poster item in your channel would probably cause performance issues though.
0 Kudos
leeladhar
Visitor

Re: How to check if a file exists in roku

Thanks RokuChris, that worked perfectly 🙂

Leeladhar
0 Kudos
leeladhar
Visitor

Re: How to check if a file exists in roku

This is the function I have written for this

Function fileExists(fileName As String) As Boolean

LocalFile = CreateObject("roRegex", "pkg:", "i")
LiveFile = CreateObject("roRegex", "http:", "i")

if(true = LocalFile.IsMatch(fileName))
LocalFileBrowser = CreateObject("roFileSystem")
return LocalFileBrowser.Exists(fileName)
else if(true = LiveFile.IsMatch(fileName))
print "fileName = "; fileName
LiveFileBrowser = CreateURLTransferObject(fileName)
LiveFileStatus = LiveFileBrowser.Head()

if( "200" = tostr(LiveFileStatus.GetResponseCode()) OR "304" = tostr(LiveFileStatus.GetResponseCode()))
return true
else
return false
end if
else
return false
end if

End Function

0 Kudos