createFoldersRecursively method

Future<List<FolderPathBit>> createFoldersRecursively(
  1. String folderPath
)

Converts a specified path like 'path/to/folder' to folders. Ignores already created folders, and creates requested folders within them. Returns a list of FolderPathBit

Implementation

Future<List<FolderPathBit>> createFoldersRecursively(final String folderPath) async {
  final folderRecursion = await getFolderPathAsIds(folderPath);
  final processedPaths = <FolderPathBit>[];

  int depth = 0;

  for (final folder in folderRecursion) {
    if (folder.id != null) {
      print('${folder.name} already exists, moving to the next folder down the tree...');
      processedPaths.add(folder);
    } else {
      print('creating ${folder.name} in tree...');
      final folderId = await createFolder(
        folder.name,
        parent: processedPaths.isNotEmpty ? processedPaths.last.id : null, // only folders with ID's are added to the list
      );
      processedPaths.add(FolderPathBit(folderId.name!, folderId.id, depth++));
    }
    print('processed ${folder.name}');
  }
  return processedPaths;
}