How to upload files to Google Drive with a dynamically generated path in n8n?

Hi everyone!

I’m trying to upload invoice attachments to Google Drive using n8n, but I need them to be saved in dynamically created folders based on the month, e.g. 2025/01 for January 2025, 2025/02 for February 2025, etc.

Does anyone know the best way to:

  1. Ensure the folder exists (or create it if it doesn’t) before uploading the file?
  2. Upload the file to the correct folder?

As you may have discovered already, this requires recursive creation of each folder level, and because Google Drive doesn’t really use the name of a folder (you can create many with the same name, in the same place), checking to see if something exists is a challenge too. Add to that, you need to look up the ID of each “folder” before adding a file or subfolder to it, and it turns into a complicated mess. Certainly not as easy as mkdir -p my/entire/path

This is not an n8n question, but someone else had the same idea a few years back here.

Unless someone already has it working in n8n, and posts before I can get it working, I’ll see if I can get it working in a sub-workflow that you could call.

I tried this before but didn’t quite get everything working. Here is what I had so far (adjusted to reflect your filename/path) if you want to use it as a starting point and try yourself.

Hi @grabekm90,
Basically you need to:

  1. Set the Folder Name for the current month
  2. Check if the folder already exists in google drive
  3. Create it if it doesn’t exist
  4. Upload the file to the found/newly created folder

I cobbled up a workflow and tested it just now and it worked:

See screenshots of my test




EDIT: Forgot to mention that it’s very important to turn on "Always output Data’ setting on the Search For Folder node or the workflow will not work when the folder is not found.

Always Output Data

Also if you have multiple folders with this name the file will be uploaded to a random one of them. (Folder names are not unique in Google Drive) But this should be fine as long as you don’t manually duplicate the folder.

If this helps please don’t forget to mark this comment as the solution :white_check_mark:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.