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:
Ensure the folder exists (or create it if it doesn’t) before uploading the file?
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.
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.
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