{"record":{"id":"461d72417b3e2f11","repo":"Zie619/n8n-workflows","slug":"filename-basename-unrecognized-file-name","errorCode":null,"errorMessage":"${fileName} → ${baseName} → Unrecognized file name structure","messagePattern":"(.+?) → (.+?) → Unrecognized file name structure","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"workflows/Code/1301_Code_Extractfromfile_Automation_Triggered.json","lineNumber":167,"sourceCode":"      \"type\": \"n8n-nodes-base.merge\",\n      \"position\": [\n        1200,\n        -800\n      ],\n      \"parameters\": {},\n      \"typeVersion\": 3.1,\n      \"notes\": \"This merge node performs automated tasks as part of the workflow.\"\n    },\n    {\n      \"id\": \"cd23e23c-9bb7-4b8d-90ab-8917783cf1ab\",\n      \"name\": \"Format Data\",\n      \"type\": \"n8n-nodes-base.code\",\n      \"position\": [\n        1420,\n        -800\n      ],\n      \"parameters\": {\n        \"jsCode\": \"const result = {};\\n\\nfor (const item of items) {\\n  const fileName = item.json.fileName || item.json.name || 'unknown_file';\\n  const baseName = fileName\\n    .split('.')[0]\\n    .replace(/\\\\s+/g, '_')\\n    .toLowerCase()\\n    .replace(/\\\\s*\\\\(\\\\d+\\\\)$/, '')\\n    .replace(/_+$/, '')\\n    .trim();\\n\\n  // regex → result key\\n  const map = [\\n    { key: 'search_terms', regex: /search_term/ },\\n    { key: 'campaigns',    regex: /campaign/     },\\n    { key: 'targeting',    regex: /targeting/   },\\n    { key: 'placement',    regex: /placement/   },\\n    { key: 'budgets',      regex: /budget/      },\\n  ];\\n\\n  const entry = map.find(m => m.regex.test(baseName));\\n  const mappedKey = entry ? entry.key : null;\\n\\n  console.log('fileName:', fileName);\\n  console.log('baseName:', baseName);\\n  console.log('mappedKey:', mappedKey);\\n\\n  if (!mappedKey) {\\n    throw new Error(`${fileName} → ${baseName} → Unrecognized file name structure`);\\n  }\\n  result[mappedKey] = result[mappedKey] || [];\\n  result[mappedKey].push(item.json);\\n}\\n\\nreturn [{ json: result }];\\n\\n\\n\\n\"\n      },\n      \"typeVersion\": 2,\n      \"notes\": \"This code node performs automated tasks as part of the workflow.\"\n    },\n    {\n      \"id\": \"02172577-d867-45a4-96ea-eb105169deff\",\n      \"name\": \"Set fileName\",\n      \"type\": \"n8n-nodes-base.set\",\n      \"position\": [\n        320,\n        -800\n      ],\n      \"parameters\": {\n        \"options\": {\n          \"dotNotation\": true,\n          \"ignoreConversionErrors\": false\n        },\n        \"assignments\": {","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/workflows/Code/1301_Code_Extractfromfile_Automation_Triggered.json#L149-L185","documentation":"Thrown by the 'Format Data' Code node in an automation-triggered file-ingestion workflow. It buckets incoming files into known categories (search_terms, campaigns, targeting, placement, budgets) by regex-testing the lowercased base filename. When no regex matches, the node throws with the offending fileName and derived baseName embedded in the message.","triggerScenarios":"An uploaded file whose base name (after splitting at the first '.', lowercasing, trimming trailing digits/underscores) contains none of: 'search_term', 'campaign', 'targeting', 'placement', 'budget'. Examples: 'ad_creative_v2.csv', 'audience_list.xlsx', a temp file like '~$budgets.xlsx' split at first dot yielding '~$budgets' still matches budget, but 'report (1).json' or 'Untitled.json' do not. Also files with no extension or names like 'Budget-Final.v2.csv' (split at first dot gives 'budget-final' which still matches 'budget') — the real failures are genuinely off-vocabulary names.","commonSituations":"Marketers renaming template files ('Q4 budgts.xlsx' typo), uploading extra files (README, images, ~$ Excel lock files with the dot split producing unexpected fragments), double extensions truncating the meaningful part ('campaigns.xlsx.csv' → base 'campaigns' still matches), or new report types added to the source system that the map was never updated for.","solutions":["Rename the uploaded file so its base name contains one of the known keywords: search_term, campaign, targeting, placement, budget.","Inspect the error message's fileName → baseName chain to see exactly what base string the node derived, then fix the source of that name (uploader or naming convention).","Extend the `map` array with a new { key, regex } entry if a legitimate new file type must be supported.","Replace the hard throw with routing to a quarantine/error branch (e.g. result['unrecognized'].push) plus a notification, if unknown files should not kill the run."],"exampleFix":"// before\nif (!mappedKey) {\n  throw new Error(`${fileName} → ${baseName} → Unrecognized file name structure`);\n}\n\n// after (collect and flag instead of halting the whole batch)\nif (!mappedKey) {\n  result['unrecognized'] = result['unrecognized'] || [];\n  result['unrecognized'].push({ fileName, reason: 'no matching keyword' });\n  continue;\n}","handlingStrategy":"type-guard","validationCode":"const KNOWN = /search_term|campaign|targeting|placement|budget/;\nconst fileName = item.json.fileName || item.json.name || 'unknown_file';\nconst baseName = fileName.split('.')[0].replace(/\\s+/g, '_').toLowerCase();\nconst recognized = KNOWN.test(baseName);","typeGuard":"function classifyFile(baseName) {\n  const map = [\n    { key: 'search_terms', regex: /search_term/ },\n    { key: 'campaigns', regex: /campaign/ },\n    { key: 'targeting', regex: /targeting/ },\n    { key: 'placement', regex: /placement/ },\n    { key: 'budgets', regex: /budget/ }\n  ];\n  return map.find(m => m.regex.test(baseName))?.key ?? null;\n}","tryCatchPattern":"for (const item of items) {\n  const key = classifyFile(baseNameOf(item));\n  if (!key) { result['unrecognized'] = result['unrecognized'] || []; result['unrecognized'].push(item.json); continue; }\n  (result[key] = result[key] || []).push(item.json);\n}","preventionTips":["Publish and enforce a file-naming convention with the five keywords.","Route unrecognized files to a quarantine bucket plus a Slack/email alert instead of throwing.","Keep the keyword map in one place so adding a report type updates a single array.","Strip Excel lock files (~$ prefix) and thumbnails before they reach this node."],"tags":["n8n","file-processing","regex","code-node","data-routing"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}