CreateFolder in GDrive, how to typecast TCloudItem

Hello, 

if not exist I want to create a folder structure on GDrive, like /myApps/myAppName/data/. 
I check if a folder exist and if not, then create it. If the folder exists then go into the folder and search fr the next part of the folder structure
        
That's my procedure:

const
  cFolderGDrive := 'myApps/myAppName/data/';
//..
FolderParts := explode('/', cFolderGDrive);
CheckAndCreateFolders('', AdvGDrive, NIL, FolderParts);
//..  
procedure CheckAndCreateFolders(ParentFolderID : string; AdvGDrive : TAdvGDrive; xFoundCI: TCloudItem; FolderParts : TArray<String>; Level : integer = 0);
var
  found: Boolean;
  i: Integer;
  fp: Integer;
  FolderID : String;
  FoundCI : TCloudItem;
begin
  found := false;

  FoundCI := TCloudItem.Create(nil);
  FoundCI.Assign(TGDriveItem(xFoundCI));

  try
    If AdvGDrive.SearchFolder(FolderParts[Level], true) then  // found foldername?
    begin
      If AdvGDrive.Drive <> NIL then              
        for i := 0 to AdvGDrive.Drive.Count-1 do  // search in entries
          If (AdvGDrive.Drive.Items.ItemType = ciFolder ) then // Folder?
          begin
            // ist es der erste Durchlauf & RootElement bzw. sind die ID vom Vater und Kind gleich
            If (
               ((Level = 0) and (AdvGDrive.Drive.Items as TGDriveItem).ParentRoot)
            or (ParentFolderID = (AdvGDrive.Drive.Items as TGDriveItem).ParentID)
            ) then
            begin
              // found a part of the structure
              found := true;
              Inc(Level);

              FolderID := (AdvGDrive.Drive.Items as TGDriveItem).ID;
              FoundCI.Assign(TGDriveItem(AdvGDrive.Drive.Items));

              if (Level < Length(FolderParts)) then
              begin
                // search next part
                CheckAndCreateFolders( FolderID, AdvGDrive, AdvGDrive.Drive.Items as TGDriveItem, FolderParts, Level)
              end
              else
              begin
                // ...
              end;
            end;
          end;
    end;

    if not found then
    begin
        for fp := Level to Length(FolderParts)-1 do
          FoundCI := AdvGDrive.CreateFolder(FoundCI, FolderParts[fp]);
// ....
    end;
  finally
     FoundCI.Free;
  end;
end;
        
        
The AdvGDrive.CreateFolder throws an EInvalidCast exception in line 320: Folder := ci as TGDriveItem;
        
How do I have to cast the TCloudItem, that no exception will be thrown?
        

Hi,


Can you try using a TGDriveItem instead of a TCloudItem object as the first parameter of the CreateFolder call?

Hi,


the function TAdvCustomGDrive.CreateFolder() needs a TCloudItem object. If I use FoundCI as TGDriveItem I will get the compile error "incompatible types 'TGDriveItem' and 'TCloudItem'.

When you use TAdvGDrive, you can cast the TCloudItem to a TGDriveItem, i.e.


gdriveitem := TGDriveItem(AdvGDrive.CreateFolder(FoundCI, FolderParts[fp]));
Bruno Fierens2018-07-19 23:45:35
Sorry, I believe there is a misunderstanding:

I don't have problems with the result of the AdvGDrive.CreateFolder()
I have problems with the handing over of the first parameter. The first call of the function creates the EInvalidCast error. I don't get a result.

Thats why there has to be a problem with the initiation of the variable FoundCI.



I solved the problem. I changed


FoundCI := TCloudItem.Create(nil);
to
FoundCI := TGDriveItem.Create(nil);

Thanks for informing a solution was found.