Recursive operation - "While" function - Copy Google drive directory

Hi, I’d like to breifly share my scenario that I created to copy a google drive template folder with all its subfolders.

Google drive doesn’t have option to copy folders. There are some tools out there. I based my method off this article he has a great tool for manual duplications.

Outline of method:

  1. Get the source folder and the destination folder
  2. Make a new folder with the same name as the source folder in the destination folder
  3. List all the files in the source folder, copy them, and move them to the new folder you created
  4. List all the folders in the source folder
  5. For folder you listed, run this process for each one.

So, the difficulty I encountered was how to implement recursion (step 5). Since the template folder has unknown levels of subfolders, how do we setup a scenario to repeat for every new subfolder it finds in the template directory?

I thought of the repeat module, thinking I can setup a loop of actions, and create a router with filter before the repeat to drop out and exit the loop whenever a condition is met (no more subfolders).

But its insufficient for this task, since the repeat value cannot be changed once initiated, that is, even if you pass in a var, once the repeat module starts, the var won’t update, since the repeat doesn’t actually execute the modules before and update its input.

Before giving up, I found another community article which was the aha moment. Use a webhook and HTTP!! uh why didn’t I think of that?!

In short, start with a webhook, create the workflow modules, then at the end use HTTP to call the webhook again (passing in new results), this will run the whole thing again. Now, just implement a filter to stop the HTTP from running infinitely. (I didn’t need to implement a filter in the end, since the 3rd module from scenario 2 would return 0 results, it won’t iterate anything, and the http won’t get called again).

See attached images with the 2 scenarios. If you’d like me to share anything else, please comment.

Scenario 1

Scenario 2 (recursive)


That looks pretty sweet!
Quick Question/confirmation.

Scenario 1: The HTTP/Webhook call from scenario 1 hits scenario 2s webhook.
Scenario 2: And then the HTTP/webhook call from scenario 2 hits scenario 1s webhook.
repeating ‘n’ times till all the subfolders / files are copied?

Or did I get that wrong?

The HTTP in scenario 2 hits the webhook in scenario 2, not 1.

So webhook in scenario 2 is hit once by the scenario 1 http, and then later from the http in scenario 2.

Scenario 1 is to create the new main (top level) folder and it’s files only (as well as some other operations).

Scenario 2 is to create the subsequent sub-folders, and it’s files. Then passing back the sub-sub-folder and doing the same thing…

1 Like