Populate listbox with filenames BUT delete "garbage file" first

My code for populating the list box with filenames is working fine, but now I need to make a slight adjustment. Sometimes my app will create a “garbage” file which needs to be cleaned up before the list is populated.

Deleting the garbage file does work, but it’s still shown in the listbox on first open. Opening the list on the second ocassion works as expected - which does make some sense to me.

However, the garbage file should never be shown in the first place.

This is the code.

[code]
Dim testName As String
listSavedFiles.DeleteAllRows // first clear the previous list
Dim filecount As Integer = myFolder.Count

For i As Integer = 1 To filecount
  
  Dim f As FolderItem = myFolder.Item(i)
  
  If f <> Nil AND f.Name = "garbage.final" Then  // first checks, then deletes if garbage file "garbage.final" exists.
    f.Delete
    MsgBox ("Blank garbage.final file found, but exists no more.")
  End If
  
  If f <> Nil AND f.Name.Right(6) = ".final" Then //Only show *.final files in the listbox
    
    //----------------------------------------POPULATE THE LISTBOX-------------
    testName = f.Name        
    listSavedFiles.AddRow(testName)
    //----------------------------------------------------------------------------------------      
  End If
Next[/code]

Some bits of the code were removed for clarity - hopefully not the important bits.

Cheers.

No need to create a 2nd folderitem.

[code]if myFolder <> Nil Then
Dim filecount As Integer = myFolder.Count

For i As Integer = 1 To filecount
  
  If  myFolder.Item(i).Name <> "garbage.final" AND myFolder.Item(i).Name.Right(6) = ".final" Then    
    listSavedFiles.AddRow(myFolder.Item(i).DisplayName)
  End If

Next i

//now delete the garbage files if you wish
//you can keep a list of the garbage files to delete during the above loop if you like. Create an array and add garbage names to it
[/code]

You were not giving the system the opportunity to delete your files during the tight loop.

[code]Dim testName As String
dim garbagefile as folderitem

listSavedFiles.DeleteAllRows // first clear the previous list
Dim filecount As Integer = myFolder.Count

For i As Integer = 1 To filecount
  
  Dim f As FolderItem = myFolder.Item(i)

  
  If f <> Nil AND f.Name.Right(6) = ".final" Then //Only show *.final files in the listbox
    if  f.Name = "garbage.final" then
         garbagefile = f
   else


    //----------------------------------------POPULATE THE LISTBOX-------------
    testName = f.Name        
    listSavedFiles.AddRow(testName)
   listSavedFiles.rowtag(listSavedFiles.lastrow) = f
    //----------------------------------------------------------------------------------------    
   end if  
End If
Next

if garbagefile <> nil and garbagefile.exists then
garbagefile.delete
end if[/code]

dont delete the file, just remember it.
But dont add it to the list.
After filling the list, if you found the file, delete it.

The index numbers of the files are not important. And by setting the rowtag on the added row, you can get back to the selected file instantly.

Basically what roger said, but hopefully the folderitem in the rowtag will be useful

Ok, thanks Roger and Jeff.

Looks like my original code (without that file delete) was probably not the most efficient - but nevertheless it has worked as required. I’ll have to think about this carefully before I try some edits.

I’ve done a fair amount of coding today and reached some important milestones. To celebrate, I’ve had a few beers. It’s around 8pm Sunday evening and I “dare not” touch the code anymore - we all know how that could end :slight_smile:

It annoys me now that I cannot get this happening, but best to leave it for tomorrow or so.

Cheers :slight_smile: :slight_smile:

[quote=353326:@Steve Kelepouris]Deleting the garbage file does work, but it’s still shown in the listbox on first open.

      If f <> Nil AND f.Name.Right(6) = ".final" Then //Only show *.final files in the listbox

[/quote]

f is deleted, but it is not Nil, so it’s added to the list even though it no longer exists.

Did you try the example located:

/files/FileBrowser.xojo_binary_project

recently ?

Nota: I do not understand what you called “garbage file”.

[quote=353342:@Emile Schwarz]Did you try the example located:
Nota: I do not understand what you called “garbage file”.[/quote]

The garbage file is created due to other conditions within the application. Probably due to other conditions that happen that are not completely resolved. So I guess you can put that down to not very good logic control.

I think that sometimes these things are unavoidable, and a simple ‘clean-up’ of errant files is a reasonable solution, providing that there is no detriment to the overall operation of the program.

But this issue fades into insignificance compared to what I’ve just discovered…

I use my brother as my “Alpha Tester”. We do this using Teamviewer which works great for this sort of testing, but unfortunately this has just revealed a big big issue.

Over the last week or so I’ve been concentrating on the “What If” the user does this, what if the user does that… etc… etc… only to realise that now, after testing, the user cannot do the basic thing that they should be able to do in the first place.!!!

I was concentrating too much on the “What If’s” and forgot about the basics. The program will work absolutely fine providing that the user, uses the program in a way that it shouldn’t be used.

The irony is very humorous (luckily no one pays me for this), but, that has put this little issue regarding the listbox, on the backburner.

I thought that this would be the “Icing on the cake” but only to find there is no cake!!!

Oh well, “One day a rooster, the next, a feather duster!”

Gulp!!.. Ouch!!! Gulp!! poo poo!.

???

You can get an eye on this example too:
Hierarchical Listbox Example .

This example uses many background colors, display invisible items (with a pink background), and uses a hierarchical entry (if the item is a folder).

The example is not locked and can be used at will. It have been downloaded 145 times… since I shared it in this forum on 2015-08-08.

Seems to work with Xojo 2017r2.1. (El Capitan).

Thanks Emile, much appreciated but that isn’t the issue at the moment. I apologise that I’ve probably complicated things by my last post.

What I am saying, is that I have realised that there is another part of my code that is broken - a big important part. Therefore my concerns about the answer to this post is not as important as it may have been.

When I have refactored my code to fix the “bigger” issue, then I will come back here and test the code that Roger, Jeff and Andrew and yourself have posted - then post my results.

The amusing thing is, is that I can see when I do fix up the other part of my code, then the “garbage” file will probably not exist and therefore this post may be redundant.

Nevertheless, I do need to know, eventually, how to resolve this particular issue. The garbage file is a temporary file that may or may not exist. I’m no expert, but I would imagine that normally these types of temporary files are ‘cleaned up’ on application start or shutdown.

In any case, for reasons explained, I cannot resolve this issue at this point.

Cheers.

So, your application polls a folder it does not own.

I hope you followed the other threads about temp files not deleted (from Xojo, bu some from other application may exists *).

  • I have an application that saves (by itself, even if this is my fault) items in Temporary.