Im trying to do is randomly pick image in my local folder and then all picked image will goes to used_image folder. Then i listed all the Picked Image and the current picked image .. then i want the IF node to do is if the allPickedImages (previous images) contains pickImages (current) .. the workflow will try again to pick another image that is not already picked.
"nodes": [
{
"parameters": {},
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
-2064,
1424
],
"id": "be04b90a-8bd5-4a69-b355-deeae1a63782",
"name": "When clicking ‘Execute workflow’"
},
{
"parameters": {
"fileSelector": "/n8n_images/nexus/image_pick/*",
"options": {}
},
"type": "n8n-nodes-base.readWriteFile",
"typeVersion": 1,
"position": [
-1712,
1456
],
"id": "df9b466d-156d-467b-8748-7f6c933e32fb",
"name": "Read Image"
},
{
"parameters": {
"fileSelector": "/n8n_images/nexus/used_image/*",
"options": {}
},
"type": "n8n-nodes-base.readWriteFile",
"typeVersion": 1,
"position": [
-1424,
1200
],
"id": "cac54177-c17d-4c62-a768-67eb6fac4232",
"name": "Read Used Images"
},
{
"parameters": {},
"type": "n8n-nodes-base.merge",
"typeVersion": 3.2,
"position": [
-896,
1504
],
"id": "e90a5b98-f4b2-444c-957b-e886133ab4c0",
"name": "Merge2"
},
{
"parameters": {
"jsCode": "const allFiles = $input.all().map(item => item.json.fileName).filter(name => name);\nreturn [{ json: { allPickedImages: allFiles } }];"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1184,
1200
],
"id": "bc91359a-7c7a-456e-bdea-e136db97d997",
"name": "Code3"
},
{
"parameters": {
"jsCode": "const files = items.map(item => item.json.fileName || item.json.path || item.json.name).filter(Boolean);\n\n// Only include .png files (case-insensitive)\nconst pngFiles = files.filter(name => name.toLowerCase().endsWith('.png'));\nif (pngFiles.length === 0) throw new Error(\"No PNG files found.\");\n\nconst randomIndex = Math.floor(Math.random() * pngFiles.length);\nreturn [{ json: { pickedImage: pngFiles[randomIndex] } }];"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1504,
1472
],
"id": "302101ca-bf04-4b72-a015-9252acf1168f",
"name": "Get Random Image"
},
{
"parameters": {
"operation": "write",
"fileName": "=/n8n_images/nexus/used_image/{{ $json.fileName }}",
"options": {}
},
"type": "n8n-nodes-base.readWriteFile",
"typeVersion": 1,
"position": [
-384,
1184
],
"id": "bae9db14-02cc-4bf0-b29f-ff759d253b3c",
"name": "Write Picked Image"
},
{
"parameters": {
"fileSelector": "=/n8n_images/nexus/image_pick/{{ $json.pickedImage }}",
"options": {}
},
"type": "n8n-nodes-base.readWriteFile",
"typeVersion": 1,
"position": [
-1328,
1488
],
"id": "97c3fb8f-396a-4645-ada2-5059455858f8",
"name": "Read Image1"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "loose",
"version": 2
},
"conditions": [
{
"id": "36a0b924-33c7-4a02-a809-649f777ddb18",
"leftValue": "={{ $json.allPickedImages }}",
"rightValue": "={{ $json.pickImages }}",
"operator": {
"type": "string",
"operation": "contains"
}
}
],
"combinator": "and"
},
"looseTypeValidation": true,
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
-688,
1504
],
"id": "14ce5a4e-584f-4cfd-bdf5-7fd4eb5e760f",
"name": "If"
},
{
"parameters": {
"fileSelector": "=/n8n_images/nexus/image_pick/{{ $json.fileName }}",
"options": {}
},
"type": "n8n-nodes-base.readWriteFile",
"typeVersion": 1,
"position": [
-544,
1328
],
"id": "042d608a-1d28-4204-bf0a-a531df8f730c",
"name": "Read Image2",
"alwaysOutputData": true
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "ab0909da-1bb3-440a-93ab-e7d9777e01d6",
"name": "pickImages",
"value": "={{ $json.fileName }}",
"type": "string"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-1152,
1504
],
"id": "38489dad-4908-441a-9b70-0d3f2541bfc0",
"name": "Edit Fields2"
}
],
"connections": {
"When clicking ‘Execute workflow’": {
"main": [
[
{
"node": "Read Image",
"type": "main",
"index": 0
},
{
"node": "Read Used Images",
"type": "main",
"index": 0
}
]
]
},
"Read Image": {
"main": [
[
{
"node": "Get Random Image",
"type": "main",
"index": 0
}
]
]
},
"Read Used Images": {
"main": [
[
{
"node": "Code3",
"type": "main",
"index": 0
}
]
]
},
"Merge2": {
"main": [
[
{
"node": "If",
"type": "main",
"index": 0
}
]
]
},
"Code3": {
"main": [
[
{
"node": "Merge2",
"type": "main",
"index": 0
}
]
]
},
"Get Random Image": {
"main": [
[
{
"node": "Read Image1",
"type": "main",
"index": 0
}
]
]
},
"Write Picked Image": {
"main": [
[]
]
},
"Read Image1": {
"main": [
[
{
"node": "Edit Fields2",
"type": "main",
"index": 0
}
]
]
},
"If": {
"main": [
[
{
"node": "Read Image2",
"type": "main",
"index": 0
}
],
[
{
"node": "Get Random Image",
"type": "main",
"index": 0
}
]
]
},
"Read Image2": {
"main": [
[
{
"node": "Write Picked Image",
"type": "main",
"index": 0
}
]
]
},
"Edit Fields2": {
"main": [
[
{
"node": "Merge2",
"type": "main",
"index": 1
}
]
]
}
},
"pinData": {},
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "5b83e5572a86d40202b88cad522e9735a04f25d61d28168bf40ead0bedde4a46"
}
}