Thank you for the sample posted. Will look a bit closer at this today. However I do see a problem with your checking if the database is registered. The registered name is not good for this verification. A database file name is not necessarily the registered name. The registered name can be whatever you want. You cannot have two registered items with the same name even if they are in different locations. The important issue here is the location of the registered Base file. Even if the name is justified (which it is not) if the file was moved, that registration is no longer valid. It need to be registered again and the old should be removed.
See my answer in this post → BASIC+Firebird: How to list non-registered .odb’s ?
Edit:
All else seems to work fine. Only issue I see is the registration.