# \[ASK\] Read Datalog using Node red

**URL:** <https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772>\
**Category:** General\
**Created:** [13 June 2018 02:59 UTC](https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772 "2018-06-13T02:59:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aribriant](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aribriant](https://discourse.nodered.org/u/aribriant)\
**Post date:** [13 June 2018 02:59 UTC](https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772/1 "2018-06-13T02:59:19Z")

</div>

Hii Guys

I would like to read the datalog of my machine. The datalog is using CSV and I just want to read the last line.  
Im facing some problem to do that.

1. My datalog filename is changing over weeks. By example it now is week 23 2018 the filename is “Log1823” file name is changing over time.

2. I would like to check my latest line using fastCSV. and i still try to explore it.

My code as follows :

`[{"id":"9e324255.c4cc3","type":"fastcsv","z":"9e7f522.6e5d5b","name":"fastcsv","headers":true,"headerstr":"","ignoreEmpty":true,"discardUnmappedColumns":true,"strictColumnHandling":false,"delimiter":",","quote":"\"","escape":"\"","comment":"#","ltrim":false,"rtrim":false,"rowDelimiter":"\\n","includeEndRowDelimiter":false,"quoteHeaders":false,"quoteColumns":false,"x":780,"y":240,"wires":[[]]},{"id":"4ff34b14.fbd8a4","type":"inject","z":"9e7f522.6e5d5b","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":220,"wires":[["a9e0d871.9c4be8"]]},{"id":"6f6ec532.c1b0ec","type":"file in","z":"9e7f522.6e5d5b","name":"","filename":"","format":"utf8","chunk":false,"sendError":false,"x":510,"y":280,"wires":[["9e324255.c4cc3","d330cc78.0c9b"]]},{"id":"a9e0d871.9c4be8","type":"function","z":"9e7f522.6e5d5b","name":"FilenameCoordinator","func":"msg.payload = \"D:\\log_rotary_common_tester_1743.csv\"\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":140,"wires":[["cb25d4d1.cbbe98"]]},{"id":"d330cc78.0c9b","type":"debug","z":"9e7f522.6e5d5b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":740,"y":140,"wires":[]},{"id":"cb25d4d1.cbbe98","type":"change","z":"9e7f522.6e5d5b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"filename","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":300,"wires":[["6f6ec532.c1b0ec"]]}]`

Thanks in advance

Really appreciate your kind help to fix my problem. 😃

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 June 2018 10:52 UTC](https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772/2 "2018-06-13T10:52:17Z")

</div>

Here is some code that you can use to determine the week of the year:

```auto

Date.prototype.getWeek = function() {
  var onejan = new Date(this.getFullYear(),0,1);
  return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
var today = new Date();
var week_of_year = today.getWeek();
msg.payload = week_of_year;
return msg;

```

So you should be able to build the file name getting the last two digits of the year and concatinating that with "Log " and the year and week.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [13 June 2018 12:22 UTC](https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772/3 "2018-06-13T12:22:29Z")

</div>

FYI, you can also generate the filename using the `moment` node – inject the current timestamp, using the format string `\LogYYww`, and voila…

```
[{
    "id": "975e4454.2e6378",
    "type": "moment",
    "z": "f05a42da.00364",
    "name": "LogYYww",
    "topic": "",
    "input": "",
    "inputType": "msg",
    "inTz": "America/New_York",
    "adjAmount": 0,
    "adjType": "days",
    "adjDir": "add",
    "format": "\\LogYYww",
    "locale": "en_US",
    "output": "",
    "outputType": "msg",
    "outTz": "America/New_York",
    "x": 340,
    "y": 380,
    "wires": [
        [
            "6f64e755.1ce9e8"
        ]
    ]
},
{
    "id": "38599fdc.793ab",
    "type": "inject",
    "z": "f05a42da.00364",
    "name": "",
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 160,
    "y": 380,
    "wires": [
        [
            "975e4454.2e6378"
        ]
    ]
},
{
    "id": "6f64e755.1ce9e8",
    "type": "debug",
    "z": "f05a42da.00364",
    "name": "",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "x": 530,
    "y": 380,
    "wires": []
}]
```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 June 2018 12:46 UTC](https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772/4 "2018-06-13T12:46:38Z")

</div>

@shrickus - man I hate it when I forget to look for a node that already exists that will solve the problem 🙄

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [13 June 2018 13:08 UTC](https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772/5 "2018-06-13T13:08:28Z")

</div>

Hey, there are always multiple solutions, and yours does not require any 3rd party nodes to be installed. Is that the formula used to calculate the ISO week, or something else? Seems like that should be part of the Date object itself…

But whenever I need to fiddle with date arithmetic, calculate timespans, or output human readable data/time formats, my go-to library is “moment”. It’s the only way I know of to reliably handle the vagaries of leap years, time zones, daylight savings time, and random leap second adjustments.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [13 June 2018 13:58 UTC](https://discourse.nodered.org/t/ask-read-datalog-using-node-red/772/6 "2018-06-13T13:58:46Z")

</div>

Once you have determined the log file name (using some of the suggestions above), the simplest way I can think of to get the last line is to use the unix `tail -1 LogYYww` command.

Put this into an `exec` node (unless you are on windows) and it will instantly retrieve just the last line of text -- no csv parsing required (fast, or otherwise):

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/337471fd99a8c9f9f439dd36ee3c63eec25fffa1.png)

Then just send the complete path to the log file in your `msg.payload` -- the output payload should be the text string you need.

Alternately, instead of building the log file name and polling for the latest data, take a look at using the `watch` node (`wfwatch` on windows) to look for changes to the log directory -- whenever a new file arrives, you will get a msg with the filename already in it.
