Dynamically add a filename when uploading a file to google drive

Hi all,

I’m currently building a Personal Assistant workflow in n8n that uses:

OpenAI (GPT-3.5) to process Tavily web search results and generate clean summaries,

Followed by a Google Drive node (Create file from text) to save those results,

With dynamic file naming based on the summary title.

What’s working so far:

The GPT node (set to Text output mode) returns results like this:

Recent News on Autonomous Agents

The full text is saved successfully as file content in Drive.

The confirmation message is returned correctly to the user. issue is file name always returns to default.

In the Google Drive node’s File Name field, I’m using this expression:

js

Copy

Edit

{{ ($json.text.match(/^ *## +(.+)/m) || [null, ‘WebSearch’])[1] }}_{{ $now.format(“YYYY-MM-DD_HH-mm”) }}.txt

However, even when the GPT response clearly includes a markdown heading (e.g., ## Recent News on Autonomous Agents), the Drive file still gets named like:

WebSearch_2025-07-30_15-20.txt

I’ve confirmed GPT is returning a plain text block, verified that the heading is on the first or second line.

Also tried other match patterns like:

$json.text.split(‘\n’).find(line => line.startsWith('## '))

Verified that the text field is populated in the node.

I want the file in Drive to be named like: “Recent News on Autonomous Agents_2025-07-30.txt”

and fallback to “WebSearch” if no heading is found.

Thank you all in advance.

Here is an idea, why don’t you instruct GPT to return a structure like

{
  "text": "<document text>",
  "file_name": "<name of the file>"
}

then in your google drive node you use these two keys to populate the file content and the file name?

You could also use google drive tool if you are using an AI Agent and automagically populate this field from instructions.

Hi,
Thank you for your reply. I’ll share a screenshot of my current tool node, is this the key and value you are referring to within APP properties? Please do let me know what I need to type in each.


Also how would I instruct GPT to return that structure?

Thank you again for your help.

Here is an example of how you could make AI agent generate the file name for you as well:

thank you but from my understanding (and i might be wrong) but it seems best to use upload tool node instead of create as i’m trying to save a search result as text from tavily to my drive. i haven’t added an edit field however.

If that what you think is best - sure! You gotta do what’s best!

Thank you. So is APP properties the area you were referring to on your initial advice?

Sorry I am not sure that I understand the question. I was referring to the ai agent generating the name automatically.

forgive me. can you explain your recommendations again please? Sorry, I’m new to this

No worries at all, the initial idea was that you generate the name of the file along with the summary text, as in instead of trying to format existing pieces of data to form the name of the file, you would simply ask the AI to provide a good filename for the summary and maybe provide the format for such name.

Alternatively, as I showed later in my next post, you can also use fromAI() function to fill in the file name field and that will effectively fulfil the same idea of making AI do the work:

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