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: 
btpoole
Channel Surfer

File Exist

Was looking for a way to determine if a file exist on my server from the Roku. I came across a function created and posted in another post:
http://forums.roku.com/viewtopic.php?f=34&t=33829&p=215631&hilit=check+for+file+exist#p215658. Here is the code:

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

The script runs and gets to the line:
 LiveFileBrowser = CreateURLTransferObject(fileName)

Once it reaches this line I get an error stating:
Function Call Operator ( ) attempted on non-function. (runtime error &he0) in pk
g:/source/loaddata.brs(519)"

The filename is read from an xml file, it is not just a name but a url path to my server like:
http://xxx.xxx.x.xxx:8000/video/first.xml

From what I can tell from the error message, it thinks CreatURLTransferObect is a function and fileName is trying to be passed to it, but it does not like it for some reason. What am I missing?
Thanks
0 Kudos
2 REPLIES 2
RokuMarkn
Visitor

Re: File Exist

CreateURLTransferObject is a utility function that's included in some of the sample code. It's in the videoplayer and in the mrsstemplate samples, for example.

If you know the file you're testing is always a URL and not a local file, you don't need to do the pattern matching, you can just use the code that's in the LiveFile.IsMatch clause. And it's kind of odd that he's converting the response codes to strings before comparing them; you can just do


if LiveFileStatus.GetResponseCode() = 200 OR LiveFileStatus.GetResponseCode() = 304


Note that technically this code doesn't check whether the file exists, it checks whether the web server returns a valid response to a HEAD request. It's possible to configure a web server to return an error even though the file exists, or to return success even though the file doesn't exist, but that would be unusual.

--Mark
0 Kudos
btpoole
Channel Surfer

Re: File Exist

Thanks Mark. Don't know why I didn't realize it was basically just checking the code returned, already had something setup looking at that.
Thanks again for looking over.
0 Kudos