Get list of files inside folder in SpecialFolder.Documents.Child

I have a folder called “Images”, and inside are two images.

When I try to get a list of the files inside “Images” the file count is always zero.
How do I get a file list of inside the a folder in SpecialFolder.Documents?

The code below always tells me there’s no files inside the folder.

Var f As FolderItem = SpecialFolder.Documents.Child("Images")
Var file As FolderItem
Var a As String

If f.IsFolder Then
  a = a + "It is a folder." + EndOfLine
End If


Var fileCount As Integer = f.Count

a = a + "Has " + fileCount.ToString + " files." + EndOfLine


For i As Integer = 1 To fileCount
  file = f.ChildAt(i)
  If file <> Nil Then
    a = a + file.Name + EndOfLine
  End If
Next


MessageBox (a)

How did the images get there?
Is this on a device or the simulator?

The documents folder you are reading may not be the one you put the images into… it certainly wont be the normal one on your mac if this is running in the simulator

The app creates the folder, and Images are added to it folder through the app.

It’s the same result on the simulator and my iPhone. The app accesses the images through a list that’s also in the documents folder.

The images are there and load, I just can’t find a way to get a list of files in the images folder.

I found the answer. You have to list all the files in the SpecialFolder.Documents folder, and not any child folders.

This will not work:

Var f As FolderItem = SpecialFolder.Documents.Child("Images")

This will work

Var f As FolderItem = SpecialFolder.Documents

The code below will list all files and folders in the iOS app’s Documents folder. Files inside folders will have colons in the file name to show what folder they’re in, i.e. “Images:Flowers”

Var f As FolderItem = SpecialFolder.Documents
Var file As FolderItem
Var a As String

Var fileCount As Integer = f.Count

For i As Integer = 0 To fileCount - 1
  file = f.ChildAt(i)
  If file <> Nil Then
    If Not file.IsFolder Then
      a = a + file.Name + EndOfLine
    Else
      a = a + file.Name + " is a folder" + EndOfLine
    End If
  End If
Next

MessageBox (a)

I have a text file called StoreName, a folder named Images, and two pictures inside that folder (Waterfall, Flowers)

Result:

I’m still suspicious… that doesn’t look right.
What code do you use to save the images ’ in ’ the Images folder?
Are you building a file path using absolute or nativepath and adding the image name to that?

I normally use

var f as folderitem
For Each f In  SpecialFolder.Documents.Children

I dont use a sub folder personally, but it looks to me like your ‘child’ records are just oddly named children of documents.

Could be wrong, but…

p is a picture that is passed to the method:

Var f As FolderItem
f = SpecialFolder.Documents.Child("Images/" + newCard.storeName)

If f.Exists Then
  f.remove
End If

p.Save(f, Picture.Formats.PNG)

Maybe this is a limitation of sandboxing in iOS, or Xojo for iOS?

I’m no file/folder expert, but it looks like that will create a file under Documents and not under Images.

I did a test with Word for Mac, I have a testios folder under Documents and I saved the file as testios/test
image

the test file didn’t save under testios but a testios/test file is created under Documents.

Now the Words window shows the file as testios:test
image

Maybe is the same as what you are getting? (a file with : between Images and their name, instead of a file inside the Images folder)

1 Like

I believe the path should be

f = SpecialFolder.Documents.Child("Images").Child(newCard.storeName)
1 Like

You’re right.

After a few changes, I found the correct way to save to the Images folder.

f = SpecialFolder.Documents.Child("Images").Child(name)

If f.Exists Then
  f.remove
End If

p.Save(f, Picture.Formats.PNG)

It’s the first time I’ve had to save a file to a sub folder - usually they’re applicationdata, or picked by save dialog box.

2 Likes

Are you building a file path .. and adding the image name to that?
Glad you sorted it.

1 Like

Indeed, this syntax won’t create a subfolder. “/” is actually a valid character in a Mac filename (try in Finder if you want), which is sometimes shown as “:”, as you’ve discovered.