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

**URL:** <https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 November 2023 12:31 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757 "2023-11-10T12:31:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmarkrol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lmarkrol/32/86532_2.png) [@lmarkrol](https://discourse.nodered.org/u/lmarkrol)\
**Post date:** [10 November 2023 12:31 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/1 "2023-11-10T12:31:56Z")

</div>

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 :

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 November 2023 13:03 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/2 "2023-11-10T13:03:01Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a5dc53a77dd7cb1b2fde0e18e188027f3ac7fe8.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/02af389af79706a8baf28174c04adbd79c4522c4.png)

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.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [10 November 2023 13:10 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/3 "2023-11-10T13:10:24Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48780d2005d51b5c24ec541dc7fe2f4e81326886.png)
- Use something like the below as the expression

```auto
'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.

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

```

`D:/Pictures/1699621787353.jpg`

---

<div class="post-metadata">

**Author:** ![lmarkrol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lmarkrol/32/86532_2.png) [@lmarkrol](https://discourse.nodered.org/u/lmarkrol)\
**Post date:** [10 November 2023 13:16 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/4 "2023-11-10T13:16:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lmarkrol](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lmarkrol/32/86532_2.png) [@lmarkrol](https://discourse.nodered.org/u/lmarkrol)\
**Post date:** [10 November 2023 13:19 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/5 "2023-11-10T13:19:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [10 November 2023 13:24 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/6 "2023-11-10T13:24:03Z")

</div>

I think it's all UTC time.

You can set the timezone.

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

```

see here  
[Date/Time functions · JSONata](https://docs.jsonata.org/date-time-functions#frommillis)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 November 2023 14:32 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/7 "2023-11-10T14:32:26Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 November 2023 14:32 UTC](https://discourse.nodered.org/t/how-to-create-write-file-with-timestamp-as-a-name-file-without-overwritting/82757/8 "2023-11-24T14:32:49Z")

</div>

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