1. Opening Problem Statement
Meet Xavier, a project manager responsible for organizing thousands of project files in Google Drive. Every quarter, Xavier has to manually create complex nested folder structures such as testXavier/2024/Q4/03 Documenten to keep everything organized. This repetitive, error-prone task easily takes him over an hour each time, and any missed folder creates havoc later when files can’t be properly filed or found.
Imagine losing precious work hours every week because of repetitive folder management tasks, compounded by occasional human errors causing misplaced documents. Xavier’s frustration grows as these mundane but crucial tasks pile up.
2. What This Automation Does
This n8n workflow is designed to automate the creation of nested folders in Google Drive by taking a single folder path string and creating each folder level if it doesn’t exist. When triggered, the workflow:
- Accepts a root folder ID (such as “root” for your main Drive) and a full desired path string like
Projects/Clients/Reports. - Splits the path into individual folder names to handle them one-by-one.
- Queries your Google Drive to check if each folder level exists under the previous one.
- Creates any missing folders along the path to ensure the entire nested structure exists.
- Returns the Google Drive folder ID of the deepest folder created or found.
- Can be called from other workflows as a reusable subroutine to manage folder creation on the fly.
This can save Xavier several hours per quarter by eliminating tedious manual folder creation and avoids errors that come from creating folders in the wrong locations or skipping nested levels.
3. Prerequisites ⚙️
- n8n automation tool with an active account (self-hosting option available for advanced users).
- Google Drive account with API credentials authorized in n8n (OAuth2 credentials set up).
- Basic familiarity with how to trigger workflows in n8n.
4. Step-by-Step Guide
Step 1: Set up the Manual Trigger Node
In your n8n editor, click + Add Node → search for and select Manual Trigger. This node listens for manual workflow testing or external triggers.
You don’t need to configure any parameters here. Testing the workflow will start it from this node.
Expected outcome: When clicking “Execute Workflow” you’ll see the workflow start from here.
Common mistake: Forgetting to add a trigger node, which means your workflow won’t start.
Step 2: Provide Dummy Input Data with Set Node
Add a Set node connected to the manual trigger. In this node, you input your test parameters:
google_drive_folder_id: Set this torootto start at your Drive root.desired_path: Enter a path string such astestXavier/2024/Q4/03 Documenten.
This simulates the data the workflow accepts.
Expected result: You should see the JSON with these fields when executing this node.
Common mistake: Misspelling field names or forgetting to set both fields.
Step 3: Split the Desired Path into Folder Names
Add a Code node named “Split the desired path.” Use the following JavaScript to split the string into an array:
// Split the desired_path string into array
$input.item.json.desired_path = $input.item.json.desired_path.split('/');
return $input.item;
This breaks up your folder path into parts to process individually.
Expected: JSON now includes an array field desired_path with each folder name.
Common error: Using wrong variable names, causing the script to fail.
Step 4: Prepare Data for Path Creation Loop
Add another Code node named “Create desired path” to pass current folder ID and the split path forward:
return {
google_drive_folder_id: $json.google_drive_folder_id,
desired_path: $json.desired_path,
};
This ensures your workflow has the correct data structure for the upcoming folder checks and creation.
Step 5: Check if Top-Level Folder Exists in Google Drive
Add a Google Drive node named “Check if top folder exists.” Configure it to search for folders under the current google_drive_folder_id matching the name desired_path[0].
- Resource: File/Folder
- Operation: List Files
- Query:
= {{ $json.desired_path[0] }} - Filter by Folder ID:
{{ $json.google_drive_folder_id }}
This node queries if the next folder already exists.
Expected output: Folder data if it exists, or empty JSON if not.
Common issue: Incorrect OAuth credentials will cause API errors.
Step 6: Decide to Create Folder or Skip
Use an If node named “If top folder doesn’t exist” with the condition:
- Check if the output from the previous node is empty to determine folder absence.
If true, you proceed to create a new folder. If false, you skip creation.
Step 7: Create Folder if Missing
Add a Google Drive node named “Create new subfolder.” Configure it to create a folder:
- Name:
= {{ $('Create desired path').item.json.desired_path[0] }} - Parent Folder ID:
= {{ $('Create desired path').item.json.google_drive_folder_id }} - Drive ID: Select “My Drive”
Expected: A folder is created if missing.
Common mistake: Using incorrect field references or missing Drive ID.
Step 8: Prepare for Next Loop Iteration
Use a Code node named “Set parameters for next run” with this script:
const desired_path = $('Create desired path').item.json.desired_path;
desired_path.shift();
return {
desired_path: desired_path,
google_drive_folder_id: $json.id,
}
This removes the first folder processed and sets the new current folder ID for the next check.
Step 9: Loop Until Path Fully Created
Add an If node named “If path has been completely created” to check if desired_path array is empty:
- If empty, move to return the final folder ID.
- If not, loop back to the step that creates the path.
This loop ensures each folder in the path is handled sequentially.
Step 10: Return the Final Folder ID
Use a Set node named “Return the ID of the last folder” to output the final google_drive_folder_id after all folders are created or found.
This ID is critical for downstream workflows that need to upload files into the correct folder.
5. Customizations ✏️
1. Change Root Folder ID: In the Dummy input data Set node, modify google_drive_folder_id to any folder ID other than “root” to start creating folders inside a subfolder.
2. Modify Folder Naming Convention: In the Create new subfolder Google Drive node, adjust the name field to add prefixes or suffixes (e.g., prepend “2024-” for yearly folders).
3. Return Metadata: Add additional fields in the final Set node to return folder metadata like creation date or URL for convenience.
4. Batch Folder Creation: Adjust the workflow to accept multiple paths for batch folder creation by looping over an array of paths.
6. Troubleshooting 🔧
Problem: “Google Drive node returns authentication errors.”
Cause: OAuth2 credentials expired or misconfigured.
Solution: Re-authenticate Google Drive node by editing credentials in n8n and ensuring proper API access.
Problem: “Folder creation fails silently or does not create nested structure.”
Cause: Incorrect handling of folder IDs or the path array.
Solution: Verify each step outputs correct folder IDs; add debug nodes to inspect data flow.
7. Pre-Production Checklist ✅
- Test with various path strings, including deep nested folders.
- Verify OAuth credentials are valid and have Drive API permissions.
- Confirm that the final returned folder ID matches the deepest created folder.
- Backup important Google Drive folders before running in production.
8. Deployment Guide
Activate your workflow in n8n by clicking the Active toggle. This workflow can be integrated as a sub-workflow and triggered from your other automations using the Execute Workflow Trigger node.
Monitor runs from the n8n dashboard and enable any logging or alerts as desired to catch failures early.
9. FAQs
Q: Can I use a different cloud storage provider?
A: This workflow is designed specifically for Google Drive using its API.
Q: Does this use up a lot of my Google Drive API quota?
A: Creating folders is a lightweight operation, but frequent bulk runs may consume rate limits.
Q: Is my folder data secure?
A: Yes, data is managed through OAuth2 with your Google account permissions. n8n does not store your credentials.
10. Conclusion
By building this nested Google Drive folder creation workflow in n8n, you’ve eliminated tedious manual tasks like creating multi-level folders one by one. Xavier can now save hours every quarter, avoid costly filing errors, and streamline project file organization. This automation not only improves productivity but also reduces frustration.
Next steps? Try integrating this workflow to trigger folder creation whenever new projects start or create companion automations to upload files directly into these newly created folders.
Keep experimenting with n8n and Google Drive to unlock more time-saving automations tailored to your workflow needs.