How to create/write file with timestamp as a name file without overwritting

I want to create or write new files from the screenshots taken with Puppeteer, but it won't work unless I overwrite them. What I desire is for the Node to write/create new files with the actual timestamp when the screenshot is taken.

Installed Node : @digitalnodecom/node-red-contrib-puppeteer

node red version : 3.1.0
node js version : 20.9.0

below is my flow :

[
    {
        "id": "f8bf1613b88e2463",
        "type": "tab",
        "label": "Flow 4",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c75fd69fd380cec7",
        "type": "inject",
        "z": "f8bf1613b88e2463",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 160,
        "wires": [
            [
                "75e9c362a92ed018"
            ]
        ]
    },
    {
        "id": "75e9c362a92ed018",
        "type": "puppeteer-browser-launch",
        "z": "f8bf1613b88e2463",
        "timeout": 30000,
        "slowMo": 0,
        "headless": true,
        "debugport": 0,
        "devtools": false,
        "cookies": "",
        "name": "",
        "x": 280,
        "y": 160,
        "wires": [
            [
                "021ee836ddbaab41"
            ]
        ]
    },
    {
        "id": "021ee836ddbaab41",
        "type": "puppeteer-page-goto",
        "z": "f8bf1613b88e2463",
        "name": "",
        "url": "https://lmarkrol.web.app",
        "urltype": "str",
        "waitUntil": "networkidle2",
        "x": 490,
        "y": 160,
        "wires": [
            [
                "033d8ab932f32121"
            ]
        ]
    },
    {
        "id": "033d8ab932f32121",
        "type": "puppeteer-page-screenshot",
        "z": "f8bf1613b88e2463",
        "name": "",
        "fullpage": true,
        "x": 130,
        "y": 220,
        "wires": [
            [
                "4ddb6b6db5c5f448"
            ]
        ]
    },
    {
        "id": "4ddb6b6db5c5f448",
        "type": "function",
        "z": "f8bf1613b88e2463",
        "name": "rename",
        "func": "msg.payload = \"screenshot_\" + Date.now() + \".jpg\";\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 320,
        "y": 220,
        "wires": [
            [
                "41cf289c49d28d78"
            ]
        ]
    },
    {
        "id": "2643e641bf611291",
        "type": "puppeteer-browser-close",
        "z": "f8bf1613b88e2463",
        "name": "",
        "x": 800,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "41cf289c49d28d78",
        "type": "file",
        "z": "f8bf1613b88e2463",
        "name": "",
        "filename": "D:/Pictures/[SS]",
        "filenameType": "str",
        "appendNewline": false,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "utf8",
        "x": 540,
        "y": 320,
        "wires": [
            [
                "2643e641bf611291"
            ]
        ]
    }
]

Hello @lmarkrol, welcome to the forum.

I do not use Node-red on Windows, so maybe my understanding is wong.
Can you expliain what you expect this setting to do?
image

Since I am on linux, I changed the path to /home/pi/[SS] and (not surprisingly) it created a file called [SS]

Look at the documentation in the right hand column for the write file node
image

It perhaps could be clearer but under Inputs it says filename ...
This means that you can pass the desired file pathname in msg.filename.
The file contents should be in msg.payload.

If you want each image saved as it's own file, I think overwrite is probably the correct option. Append makes no sense.

1 Like

If you want to store the screenshots with a dated filename.

I would:

  • Get rid of the function node entirely
  • Connect the screenshot node directly to the file write node (with overwrite as @jbudd said)
  • Set the Filename input to an expression
    image
  • Use something like the below as the expression
'D:/Pictures/' & $fromMillis($millis(),'[D01]-[M01]-[Y0001] [H01][m01]')  & '.jpg' 

This will result in a file named : D:/Pictures/10-11-2023 1307.jpg (and will overwrite)

You could just use $millis() also if you wanted to.

'D:/Pictures/' & $millis()  & '.jpg' 

D:/Pictures/1699621787353.jpg

1 Like

the [SS] is my folder name where I want to store the screenshot files, I don't get it why it create the files named [SS] in your linux, but thanks for the info

This appears to be work, and the date is a perfect match. However, I'm unsure if there is a time (clock) format in the file name.

10-11-2023 1310.jpg

Is 13:10 the actual time? If it is, how can I modify it to match my GMT?

I think it's all UTC time.

You can set the timezone.

'D:/Pictures/' & $fromMillis($millis(),'[D01]-[M01]-[Y0001] [H01][m01]','-0100')  & '.jpg'

see here
Date/Time functions · JSONata

1 Like

Just for info,
I find using $moment() much clearer

'D:/Pictures/' & $moment().tz("Europe/London").format("DD-MM-YYYY HHmm")  & '.jpg'
2 Likes

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