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: 
dynamitemedia
Binge Watcher

for loop question

i want to use a for loop to get video files from a list.
example file list:
    video-1.mp4
    video-2.mp4
    video-3.mp4
    video-4.mp4


this works to get the "number" of the videos i want.
videoNum=0
for i=1 to 4
videoNum = videoNum +1
print "video number is"; videoNum
end for

but this only returns the "Number", so what do i need to do to add a number at the end of the video file?

i have tried adding a few different ways but i always get a type mismatch error.

arrays and loops confuse me so much at times, even in php... any help would be appreciated
Twitter: iptvmyway facebook: iptvmyay
Channels: Warriors of War, Go Fight Live, Heading Outdoorz, IPTVmyway
0 Kudos
8 REPLIES 8
kbenson
Visitor

Re: for loop question

I'm confused as to whether you have the names and know the format, and they are always something like "video-$NUMBER.mp4", or whether you have a list of files, and just want to iterate through them.

For a known format, where the number is a monotonically increasing integer:

numFiles = 10
for i=0 to numFiles
filename = "video-"+i.toStr()+".mp4"
' do something
end for


For a list of files:

files = ["file1.mp4", "other.mp4", "file23534.mp4"]
for each file in files
' file contains filename from files array
' do something
end for


Or am I just completely missing your point?
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
dynamitemedia
Binge Watcher

Re: for loop question

filename = "video-"+i.toStr()+".mp4"


it was the toStr() i had left out. Thanks!

nope you hit it right on the head, will test it now...

im too busy playing ur new game 😉
Twitter: iptvmyway facebook: iptvmyay
Channels: Warriors of War, Go Fight Live, Heading Outdoorz, IPTVmyway
0 Kudos
kbenson
Visitor

Re: for loop question

"dynamitemedia" wrote:
filename = "video-"+i.toStr()+".mp4"


it was the toStr() i had left out. Thanks!


There's also the stri() function, so you can do

filename = "video-"+stri(i)+".mp4"
but I prefer the interface methods.


im too busy playing ur new game 😉


🙂
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
dynamitemedia
Binge Watcher

Re: for loop question

now i have another question about this...

how could i do this with out knowing how many files there are? and want to get that number dynamically

numFiles = 10

say i want to check this folder for videos or images in this folder tmp:/new/
with image or video names like this:
    image-01
    image-02

or
    video-01
    video-02

i know in php i can do a count of files with that name, but how could i do that here? so it would then come back something like this:
filename = "image-"
whatever here to get the count of "filename" ending up with something like this
count =
numFiles = count

would it need the extension of the file? like .mp4 or .jpg ?
Twitter: iptvmyway facebook: iptvmyay
Channels: Warriors of War, Go Fight Live, Heading Outdoorz, IPTVmyway
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: for loop question

roFileSystem::Find() might be a simpler way to accomplish what you're trying to do. The first parameter is the directory you want to search and the second is a regular expression that files must match to be included in the result, so to find all .mp4 or .jpg files in tmp:/new, you might say this...

fs = CreateObject("roFileSystem")
files = fs.Find("tmp:/new/", "\.(mp4|jpg)$")
0 Kudos
dynamitemedia
Binge Watcher

Re: for loop question

thanks chris, i knew there had to be a function to do that.
i am not at home to check, but what if there are multiple .jpg's but i only want the ones with the word
"newImage-" and these all have

01,02 etc on the end

could i use a " * " wildcard?

wildCard = "newImage-*.jpg"

fs = CreateObject("roFileSystem")
files = fs.Find("tmp:/new/", wildCard)

plus i will prob just need to use this for either a image or a video, but not together. but its awesome i know how to do it if i need to later!
Twitter: iptvmyway facebook: iptvmyay
Channels: Warriors of War, Go Fight Live, Heading Outdoorz, IPTVmyway
0 Kudos
kbenson
Visitor

Re: for loop question

"dynamitemedia" wrote:

could i use a " * " wildcard?


No, but you could use .*
Like this, assuming the files must BEGIN with newImage and end with .jpg.

wildCard = "^newImage-.*\.jpg$"


It uses a regular expression, not a shell expansion. That MUCH too large a topic to cover here. Luckily, there's a plethora of tutorials to get you started online.
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
dynamitemedia
Binge Watcher

Re: for loop question

yes that is a whole another can of worms!!

and thanks, that actually makes sense what you added. Yes i wanted that name exactly, just in case other jpg's or even mp4's were still hanging around... didnt want the count wrong

most of these i will do server side, but its good to know what all options i have.

i'll test when i get home if i have any more questions
Twitter: iptvmyway facebook: iptvmyay
Channels: Warriors of War, Go Fight Live, Heading Outdoorz, IPTVmyway
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.