Recursive parsing of directory structure

Hello
I would like to get all files and sub directories (and their files/dirs) from a starting dir folder. I’m aware that Xojo can’t handle recursive functions properly. Is there another clean way to do?

I’m not sure where you got this misconception.

This is the DeleteEntireFolder function that used to be in the documentation. It seems they built it into the framework and dropped the useful example of how to recurse from the documentation.

DeleteEntireFolder Function
Function DeleteEntireFolder(theFolder As FolderItem, continueIfErrors As Boolean = False) As Integer
  // Returns an error code if it fails, or zero if the folder was deleted successfully
  
  Dim returnCode, lastErr, itemCount As Integer
  Dim files(), dirs() As FolderItem
  
  If theFolder = Nil Or Not theFolder.Exists() Then
    Return 0
  End If
  
  // Collect the folder‘s contents first.
  // This is faster than collecting them in reverse order and deleting them right away!
  itemCount = theFolder.Count
  For i As Integer = 1 To itemCount
    Dim f As FolderItem
    f = theFolder.TrueItem(i)
    If f <> Nil Then
      If f.Directory Then
        dirs.Append(f)
      Else
        files.Append(f)
      End If
    End If
  Next
  
  // Now delete the files
  For Each f As FolderItem In files
    f.Delete
    lastErr = f.LastErrorCode // Check if an error occurred
    If lastErr <> 0 Then
      If continueIfErrors Then
        If returnCode = 0 Then returnCode = lastErr
      Else
        // Return the error code if any. This will cancel the deletion.
        Return lastErr
      End If
    End If
  Next
  
  Redim files(-1) // free the memory used by the files array before we enter recursion
  
  // Now delete the directories
  For Each f As FolderItem In dirs
    lastErr = DeleteEntireFolder(f, continueIfErrors)
    If lastErr <> 0 Then
      If continueIfErrors Then
        If returnCode = 0 Then returnCode = lastErr
      Else
        // Return the error code if any. This will cancel the deletion.
        Return lastErr
      End If
    End If
  Next
  
  If returnCode = 0 Then
    // We‘re done without error, so the folder should be empty and we can delete it.
    theFolder.Delete
    returnCode = theFolder.LastErrorCode
  End If
  
  Return returnCode
End Function

Adapt that to your needs for display instead of removal.

2 Likes

Thanks but I won’t delete any folder. I simply want to read the whole dir/file structure and store it in an array. I also iterate through this growing array to check if additional directories (to be read as well) came in. I think the FOR loop doesn’t like a growing array it just iterates

Right, I gave you an example of how to recurse. It’s up to you to adapt it.

Sorry, I don’t see a way to adapt it.

  1. In my case there is a growing array (to be iterated till all parsed)
  2. A key moment of the delete sample is led by the error handling that can’t be used in my case.

Thanks anyway

Remove the deletion and add the files to an array. Where is the problem?

If you have lots of files you may want to use FileListMBS which has a yield parameter.

2 Likes

Adding to a growing array is as trivial as:

var SomeArray(-1) as SomeType
Some loop
   SomeArray.Add ItemToAdd
End Some loop

Copy and Paste Tim example and read where the Delete part is and adapt this to your need (remove Delete Files and Delete Directories)…

You have all files in… files and all folders in dirs

Is it clear now ?

Thanks all of you, I see now several solutions.
I was not aware the loop is able to handle a growing array but it works like a charm! It just works and seems the easiest solution, therefore I mark it as such.

My example was just for easy understanding. My final solution will be more complex. Beside of reading local file structures I also have to do it over a special FTP tool reading the full structure over shell but I think I will be handle it over the for loop.