8
n8n 中文网amn8n.com

模板 嵌套文件夹递归

高级

这是一个Content Creation, Multimodal AI领域的自动化工作流,包含 20 个节点。主要使用 If, Set, Code, Wait, GoogleDrive 等节点。 定时监控 Google Drive 文件夹中的新增和修改文件

前置要求
  • Google Drive API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "qOAgdqA9CYJiMqJl",
  "meta": {
    "instanceId": "06c3f8e59397fa4698243f5d80316ee169cf2eaec31d2e6a72a6f2440faa459c",
    "templateCredsSetupCompleted": true
  },
  "name": "模板 嵌套文件夹递归",
  "tags": [],
  "nodes": [
    {
      "id": "8fb482bf-81df-48de-b4c0-a04c3a31891d",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -416,
        -592
      ],
      "parameters": {
        "color": 6,
        "width": 1040,
        "height": 496,
        "content": "### 设置要监控的根文件夹并触发递归工作流"
      },
      "typeVersion": 1
    },
    {
      "id": "7657a4a5-9d4c-4c15-83f6-2edf8a40cca3",
      "name": "便签 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -416,
        -80
      ],
      "parameters": {
        "color": 6,
        "width": 1040,
        "height": 448,
        "content": "### 递归遍历所有嵌套文件夹并列出其文件"
      },
      "typeVersion": 1
    },
    {
      "id": "02300ebf-8389-4a7b-ba79-1977202327ba",
      "name": "循环所有子文件夹",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -96,
        128
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "3f55820c-f4f0-40fb-ac32-4e62b43ea196",
      "name": "列出文件夹",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        128,
        128
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "id",
            "value": "={{ $json.id }}"
          },
          "whatToSearch": "folders"
        },
        "options": {},
        "resource": "fileFolder",
        "returnAll": true
      },
      "typeVersion": 3,
      "alwaysOutputData": true
    },
    {
      "id": "572e0740-0bec-469d-8f4f-9eec8edb9439",
      "name": "如果存在子文件夹",
      "type": "n8n-nodes-base.if",
      "position": [
        272,
        128
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e6a6144c-5c3d-4c15-92e7-147787ece96c",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "9620aee5-78dc-424c-a68e-78c66e67cd51",
      "name": "触发下一级递归",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        464,
        128
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $workflow.id }}"
        },
        "workflowInputs": {
          "value": {},
          "schema": [
            {
              "id": "folder_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "folder_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "folder_name"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e1f951c5-8abd-4f08-ae78-49f20f12c811",
      "name": "列出根目录中的文件夹",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -64,
        -464
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "list"
          },
          "whatToSearch": "folders"
        },
        "options": {},
        "resource": "fileFolder",
        "returnAll": true
      },
      "typeVersion": 3,
      "alwaysOutputData": true
    },
    {
      "id": "77e0374f-38cf-4788-9ed6-8af4ad672294",
      "name": "列出根目录中的所有文件",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -64,
        -272
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "list"
          },
          "whatToSearch": "files"
        },
        "options": {
          "fields": [
            "*"
          ]
        },
        "resource": "fileFolder",
        "returnAll": true
      },
      "executeOnce": true,
      "typeVersion": 3,
      "alwaysOutputData": true
    },
    {
      "id": "4c07501b-6989-42c5-82ff-f15793e56f84",
      "name": "便签 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -112,
        -544
      ],
      "parameters": {
        "color": 3,
        "width": 288,
        "height": 432,
        "content": "## 说明:"
      },
      "typeVersion": 1
    },
    {
      "id": "6e141c99-9142-4480-9b31-c5eafcb387ec",
      "name": "开始递归",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -368,
        -16
      ],
      "parameters": {
        "inputSource": "passthrough"
      },
      "typeVersion": 1.1
    },
    {
      "id": "a1d1343a-b0a4-417c-8442-eabec70cb71d",
      "name": "触发第一级递归",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        224,
        -464
      ],
      "parameters": {
        "options": {
          "waitForSubWorkflow": true
        },
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $workflow.id }}"
        },
        "workflowInputs": {
          "value": {},
          "schema": [
            {
              "id": "folder_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "folder_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "folder_name"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "executeOnce": false,
      "typeVersion": 1.2
    },
    {
      "id": "5b229c5c-5662-460e-a221-19a02efd8863",
      "name": "列出所有文件",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        272,
        -32
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "id",
            "value": "={{ $json.id }}"
          },
          "whatToSearch": "files"
        },
        "options": {
          "fields": [
            "*"
          ]
        },
        "resource": "fileFolder",
        "returnAll": true
      },
      "typeVersion": 3
    },
    {
      "id": "f6099368-fe4a-4360-9bf6-f7654b1c6a60",
      "name": "便签 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        640,
        -592
      ],
      "parameters": {
        "width": 384,
        "height": 496,
        "content": "### 输出自上次生产执行以来创建或更新的文件批次"
      },
      "typeVersion": 1
    },
    {
      "id": "26a7b645-fac2-4f56-94da-d3f447b91d6a",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -368,
        -384
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 60
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f0e363fe-c83c-47ff-bffd-7368ac9874b5",
      "name": "设置开始时间",
      "type": "n8n-nodes-base.code",
      "position": [
        416,
        -272
      ],
      "parameters": {
        "jsCode": "// Get the last execution timestamp from static data\nconst workflowStaticData = $getWorkflowStaticData('global');\nconst executionStart = $('Schedule Trigger').first().json.timestamp;\n\n// At the end of successful execution, update the timestamp for future comparison\nworkflowStaticData.lastScheduleExecutionStarted = new Date(executionStart).getTime();\n\nreturn [{ json: { lastScheduleExecutionStarted: workflowStaticData.lastScheduleExecutionStarted } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "d53c8060-c97f-4577-a151-4690030cb23f",
      "name": "输出新增或更新的文件",
      "type": "n8n-nodes-base.code",
      "position": [
        800,
        -432
      ],
      "parameters": {
        "jsCode": "const recentlyChanged = [];\nconst workflowStaticData = $getWorkflowStaticData('global');\nlet lastScheduleExecutionStarted = workflowStaticData.lastScheduleExecutionStarted;\n\n// Set fallback in case this is the first execution that should return all files found\nif (!lastScheduleExecutionStarted) {\n  lastScheduleExecutionStarted = new Date('2010-01-01').getTime();\n}\n\n// Compare created and update timestamps of each file to see if it's later than the last workflow execution\nfor (const item of $input.all()) {\n  if ( (new Date(item.json.modifiedTime).getTime() > lastScheduleExecutionStarted ) || ( new Date(item.json.createdTime).getTime() > lastScheduleExecutionStarted ) ){\n    recentlyChanged.push(item);\n  }\n}\n\nreturn recentlyChanged;"
      },
      "typeVersion": 2
    },
    {
      "id": "80b1a63f-dfac-4422-8778-d0624b6a5fac",
      "name": "便签 4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        640,
        -80
      ],
      "parameters": {
        "color": 5,
        "width": 384,
        "height": 448,
        "content": "## 信息"
      },
      "typeVersion": 1
    },
    {
      "id": "728dce31-efc1-44ee-b626-7674c86aaea4",
      "name": "如果存在文件夹",
      "type": "n8n-nodes-base.if",
      "position": [
        -224,
        -16
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "5803397c-7522-4d2c-8118-a8d3da9500df",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2,
      "alwaysOutputData": false
    },
    {
      "id": "17c0b075-9784-4bba-9f5d-d0f00f851271",
      "name": "退出子工作流",
      "type": "n8n-nodes-base.set",
      "position": [
        -48,
        -32
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "7dbc41c8-9e5b-4def-bfc4-8fe9e024d70d",
              "name": "status",
              "type": "string",
              "value": "No folders found"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f2123228-8530-4dc6-8cbb-264bfd7efa3c",
      "name": "等待文件比较完成",
      "type": "n8n-nodes-base.wait",
      "position": [
        256,
        -240
      ],
      "webhookId": "8b8cfa30-3f3b-433f-a456-d5866790b18d",
      "parameters": {},
      "typeVersion": 1.1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "93075173-0789-47dd-8b14-bf6fccfa097d",
  "connections": {
    "List folders": {
      "main": [
        [
          {
            "node": "If subfolders exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SetStartTime": {
      "main": [
        []
      ]
    },
    "List all files": {
      "main": [
        [
          {
            "node": "Outputs new or updated files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start recursion": {
      "main": [
        [
          {
            "node": "If folders exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If folders exist": {
      "main": [
        [
          {
            "node": "Loop all subfolders",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Exit sub workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "List folders in root",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If subfolders exist": {
      "main": [
        [
          {
            "node": "Trigger recursion to next level",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop all subfolders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop all subfolders": {
      "main": [
        [],
        [
          {
            "node": "List folders",
            "type": "main",
            "index": 0
          },
          {
            "node": "List all files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List folders in root": {
      "main": [
        [
          {
            "node": "Trigger recursion of first level",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List all files in root": {
      "main": [
        [
          {
            "node": "Outputs new or updated files",
            "type": "main",
            "index": 0
          },
          {
            "node": "Wait for file comparison to complete",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Outputs new or updated files": {
      "main": [
        []
      ]
    },
    "Trigger recursion to next level": {
      "main": [
        [
          {
            "node": "Loop all subfolders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trigger recursion of first level": {
      "main": [
        [
          {
            "node": "List all files in root",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for file comparison to complete": {
      "main": [
        [
          {
            "node": "SetStartTime",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 内容创作, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量20
分类2
节点类型10
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Ossian Madisson

Ossian Madisson

@ossian

AI & Automation consultant with many years of experience from both devops and business management. A solid combination to really understand where technology can create real business value. My focus is to help businesses in Sweden work smarter and achieve more by applying technology in a useful and responsible way. Feel free to contact me at ossian@smultronstudio.com or schedule a short call at https://smultronstudio.com

外部链接
在 n8n.io 查看

分享此工作流