Spot the bug(s)

This code is supposed to find all files that have a .txt extension in a dir

For Each f As FolderItem In dataFolder.Children
  If f.Name.Right(3) = "txt" Then
    MainListBox.AddRow(f.NativePath)
  End If
Next

Why not compare right(4) to “.txt” instead?

What he said :wink:

(and yes, I know: I’m cheating)

Case sensitive filing system?
Uppercase the name and test for “.TXT”

test that datafolder is a folder, is not nil, and that it exists.
This could blow up quite quickly… :slight_smile:

  1. Did not crosschecked with Reference but woulnd’t this include folders with txt extensions aswell?
  2. I would check for lenght of filenames before trying to use right, left, mid statements e.g. what about a filename just named “txt”
  3. as mentioned before this is not upper- and lowercase friendly, simply convert everything to upper or lowercase before comparing
  4. this code is poor on performance because it’s interacting with a control and basically locking a program when parsing a folder with thousands of files.

The comparison in Xojo is case insensitive, so this may work on a case sensitive file system.

You can use Right/Left/Mid without bounds checking exceptions as they adjust your values. So right(3) on the text “a” gives back “a”. The newer Middle function would raise an exception.

I would say:
• Check f is not nil
• Check for the name ending in .txt rather than txt
• Check that f isn’t a directory

this is in a xojo example and seems really wrong
at least it doesnt match up with the stated purpose of the code

you’ve all basically confirmed for me that this IS a bug and does not match the stated purpose :slight_smile:

If you have deep level directory’s won’t it blow xojo’s stack if it calls itself too many times.

Will it enumerate the volumes folder correctly to scan other disks? And if so will it throw if it comes up a Linux partition ?

it would have all those problems since it just checks the folderitems name for the last 3 characters

the method isnt recursive so no
unless you’re referring to something other than my original post ?

MAybe
I havent tried that

This is from the word counter Worker example and I was poking through the code to see how it worked when I noticed this and realized that it doesnt do any checks to see hat the items IS a fIle (not a dirr etc)
If it is a directory with a name that ends in TXT things blow up since you cant open a directory as a text input stream

I’m sure there’s a “well its JUST example code” somewhere in all this
But I tend to think that if they are going to post blogs & examples they should be reasonably robust (perhaps not flawless)
In this case the example says it will count words in files named with a .txt extension - but it doesnt actually verify the extension - just the last 3 chars which is NOT the same thing) nor does it see that its a FILE (it could be a dir named MyDir.txt and things would break)

Thanks for everyones participation & observations on such a short piece of code

Yes, I find these issues on many examples in the docs - they don’t “cover all the bases”.

But then, they have established that their target audience is “simpleton developers”, so of course their examples reflect that.