Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Nov 20, 2018 3:33 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Listing specific file into listbox
PostPosted: Wed Jan 16, 2013 1:46 am 
Offline

Joined: Tue Aug 29, 2006 4:04 am
Posts: 513
Location: Indonesia
Hi,

I'm using this code to list an item of a folder into a listbox, this is working properly, but way to slow to load the file.
If the files in the folder less than 10 files, it can run faster, if its more, its really takes some time.

is there any other ways to do this operation than using this code.?

Dim f As FolderItem
Dim filetype() As String
Dim fileext,path As String
Dim i As Integer
//load path
dim t as textinputstream
dim p as string
dim ok as boolean

ok=false
f=getfolderitem("db.path") '\\server\main\
t=textinputstream.open(f)
p=t.readline
t.close
f = getfolderitem(p).child("data")
filetype = Array(".odr",".cons")

For Each fileext In filetype
For i = 1 To f.Count
If (f.Item(i).Name.Right(fileext.Len).Lowercase = fileext) Then
ListBox2.AddRow( f.Item(i).Name)
End If
Next
Next



Thanks
Regards
Rivo


Top
 Profile  
Reply with quote  
 Post subject: Re: Listing specific file into listbox
PostPosted: Wed Jan 16, 2013 4:30 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Swap the for loops.
for i = 1 to f.count
for each fileext in filetype


Top
 Profile  
Reply with quote  
 Post subject: Re: Listing specific file into listbox
PostPosted: Wed Jan 16, 2013 8:23 am 
Offline

Joined: Wed May 20, 2009 4:43 pm
Posts: 958
In addition to Tim's comment, I'd suggest adding an 'Exit For' into your 'If' block so that you don't have to continue with the rest of the filetypes once you have found the correct one.

If (f.Item(i).Name.Right(fileext.Len).Lowercase = fileext) Then
ListBox2.AddRow( f.Item(i).Name)
Exit For
End If


Also, don't know if it does much to speed up the code, but I would use a variable for the filename so that you aren't retrieving it twice from the folderObject:
Dim fileName as string
fileName = f.Item(i).Name
If (fileName.Right(fileext.Len).Lowercase = fileext) Then
ListBox2.AddRow(fileName)
Exit For
End If

_________________
RB 2009r4 Windows XP


Top
 Profile  
Reply with quote  
 Post subject: Re: Listing specific file into listbox
PostPosted: Wed Jan 16, 2013 10:52 am 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
Load the FolderItems in an array first. Using Item repeatedly will slow you down. And cache the properties so you don't have to repeatedly pull them from the object. Finally, as Tim suggested, swap the loops.

(You could do this in one loop without loading the FolderItems into an array, but this makes it easier to debug too.)
dim fileList() as FolderItem
dim folderCount as integer = f.Count
for i = 1 to folderCount
fileList.Append f.Item( i )
next
for i = 0 to fileList.Ubound
dim thisFile as FolderItem = fileList( i )
dim thisName as string = thisFile.Name
For Each fileext In filetype
dim thisExt as string = thisName.Right( fileext.Len ) // You don't need Lowercase here
if thisExt = fileext then
ListBox2.AddRow thisName
end if
next
next

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: Listing specific file into listbox
PostPosted: Wed Jan 16, 2013 12:18 pm 
Offline
Real Software Engineer

Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7858
Location: Canada, Alberta, Near Red Deer
skip looping over the extensions by using IndexOf

ok=false
f=getfolderitem("db.path") '\\server\main\
t=textinputstream.open(f)
p=t.readline
t.close
f = getfolderitem(p).child("data")
filetype = Array(".odr",".cons")

dim fileList() as FolderItem
dim folderCount as integer = f.Count
for i = 1 to folderCount
fileList.Append f.Item( i )
next
for i = 0 to fileList.Ubound
dim thisFile as FolderItem = fileList( i )
dim thisName as string = thisFile.Name
dim thisExt as string = thisName.Right( fileext.Len ) // You don't need Lowercase here
if filetype.IndexOf(thisExt) > -1 then
ListBox2.AddRow thisName
end if
next


Or use a dictionary of extensions so the lookup is faster

ok=false
f=getfolderitem("db.path") '\\server\main\
t=textinputstream.open(f)
p=t.readline
t.close
f = getfolderitem(p).child("data")
dim filetype as new Dictionary
filetype.value(".odr") = true
filetype.Value(".cons") = true

dim fileList() as FolderItem
dim folderCount as integer = f.Count
for i = 1 to folderCount
fileList.Append f.Item( i )
next
for i = 0 to fileList.Ubound
dim thisFile as FolderItem = fileList( i )
dim thisName as string = thisFile.Name
dim thisExt as string = thisName.Right( fileext.Len ) // You don't need Lowercase here
if filetype.HasKey(thisExt) then
ListBox2.AddRow thisName
end if
next

_________________
Norman Palardy (Real Software)


Top
 Profile  
Reply with quote  
 Post subject: Re: Listing specific file into listbox
PostPosted: Wed Jan 16, 2013 12:30 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
Norman, your suggestions won't work properly because fileext.Len differs among the extensions.

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: Listing specific file into listbox
PostPosted: Thu Jan 17, 2013 10:19 pm 
Offline

Joined: Tue Aug 29, 2006 4:04 am
Posts: 513
Location: Indonesia
Thanks..

all the samples are working.
swapping it help me to save another 5 second.
Now I can list the file in 4 second instead of 9 second.


Thanks

Regards,
Rivo


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group