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?