newly created folder.count = 1

EC 10.11.5
Xojo 2016r1

I just found out that when, in the Finder, I create a new folder and put into it a single file, accessing that folder from Xojo, f.count returns 1.
Just to make sure, I created several folders (putting into them a single file), but the result is always “1”.
Is there any way to “refresh” newly created folders?
Thank you.

I’d guess there’s a hidden file in the folder called .DS_Store.

Maybe I misunderstand you. You create a folder and write one file into that folder. Then you count the files in that new folder and get one? Sounds right to me. Try putting 2 files in and see if you get 2.

First of all, thank you for answering. Let me expand:

In the Finder I create a folder and drop into it a file: in Xojo f.count should be 2, that is, the .DS_Store file and the file dropped.
In fact, that is what I see with old folders: f.count is always the number of files + 1 (the .DS_Store file).

For instance, this is what shows up in a listbox if I populate it with the newly created folder:
for i as integer = 1 to f.count
listbox1.addrow f.item(i).name
The listbox shows one row only (the name of the file dropped into it), but “.DS_Store” does not appear.

The same code when used accessing an old folder shows:
the other items’ name

I think the problem is related to the System, not to Xojo. That’s why I was asking if there is a way to force the Finder to generate the .DS_Store file.

Sorry! Mac stuff that I am not competent at. I’ll skulk away now.

I don’t think the .DS_Store file can be assumed to exist. It contains some some attributes to the folder it’s in.
If not attributes are used the file does not exist.

Try creating a new folder and list it’s content in Terminal using “ls -la” (shows hidden files). There should not be a .DS_Store file.
Now, copy a file to the folder and list it’s contents again. There will be no .DS_Store file yet.

Create a new FOLDER in that folder and list it’s content again and you should see a new .DS_Store file :slight_smile:

So, you can’t assume it’s there.

This can be used not to list the .DS_Store file.

  For i as Integer = 1 to f.Count
    If f.item(i).name <> ".DS_Store" Then
      Listbox1.AddRow f.item(i).name
    End If

Thank you, Albin, for making the matter clear.
I’ll have to refactor some code telling when a folder was empty. Something like:
if f.count <= 1 then
//do something: the directory is empty
//do something else: the directory contains items
end if

Now I’ll have to parse the directory’s content. Not a big deal. Thanks again.

The .DS_Store file is only created when in Finder you change the view to icon view or the like.

@Eli Ott Thank you for the additional piece of news. Even without changing the view, look at this test:

  1. Create a new folder in Finder mode: f.count = 0
  2. Drag an item into the new folder: f.count = 1
  3. Remove the item from the new folder: f.count = 1

So, to know by code if a directory is empty (i.e. without any visible file) it requires a bit of code. At present I use this:

  dim isEmpty as Boolean
  if f.Count = 1 then
    if not f.Item(1).Visible then// its only element is .DS_Store or any other invisible item
      isEmpty = true
    end if
  end if
  if f.Count = 0 or isEmpty then
    //do something
    //do something else
 end if

.DS_Store doe not make the difference in the test I just made.

Another possibility: Do you re-create the FolderItem between the tests (either by re-starting the application or by doing f= New FolderItem())?

The folder is created manually in the finder selecting File > New Folder.

Then, the app stores its reference in a listbox:

dim dlg as new SelectFolderDialog
dim f as folderitem = dlg.ShowModalWithin(self)
if f <> nil then
  listbox1.addrow "A Folder"
  listbox1.cellTag(listbox1.lastindex,0) = f.GetSaveInfo(nil)

end if

Then, when I want to parse its content I do:

dim f as FolderItem = GetFolderItem(listbox1.CellTag(0, 0).StringValue)
if f <> nil and f.Exists then
if f.Directory then
MsgBox str(f.Count)
end if
end if
In this case, I get results as above: 0 when the folder is created; 1 when I drop an item; 1 when I remove the item.

On the other end, if I do not make use of the above code but access that folder directly via SelectFolder:
dim f as folderitem = selectFolder
if f <> nil then
msgbox str(f.count)
end if
then results are: 0 when the folder is created, 1 when I drop an item; 0 when I remove the item.

Meanwhile, thanks for testing and answering.