# Change Value on file

**URL:** <https://discourse.nodered.org/t/change-value-on-file/42273>\
**Category:** General\
**Created:** [8 March 2021 14:44 UTC](https://discourse.nodered.org/t/change-value-on-file/42273 "2021-03-08T14:44:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ita](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ita](https://discourse.nodered.org/u/Ita)\
**Post date:** [8 March 2021 14:44 UTC](https://discourse.nodered.org/t/change-value-on-file/42273/1 "2021-03-08T14:44:30Z")

</div>

Hello,  
I have the specifc file .dat (especific program).  
I am creating a dashboard.  
I can read and change values with the "change" fuction,  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a1f3315ae28b27b1c5c866d223dd3e3ccf97b18.png)

But now I need just read the especific variable on file, for an exemple, show the value on dashboard.

I do not know do this, because when I read the file I see just a big string, and I do not know search the specific variable and your value.

 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff82891001d08b830e3da073e425606cac3cc301.png)

Please help

---

<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:** [8 March 2021 15:35 UTC](https://discourse.nodered.org/t/change-value-on-file/42273/2 "2021-03-08T15:35:15Z")

</div>

I believe that this `.dat` file is really a MS Access database -- is this a custom program that is reading/writing that file?

If you try to open the file directly (not through that program) then you have to know the format of the data, and you run the risk of corrupting the file so it's no longer usable by the program -- It's safest to only use the program to read/write the data.

That being said, the format does appear to be a newline separated (mostly) text file, so nothing stops you from parsing the text into lines, or using a regex to search for the value you need...

---

<div class="post-metadata">

**Author:** ![Ita](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ita](https://discourse.nodered.org/u/Ita)\
**Post date:** [8 March 2021 16:03 UTC](https://discourse.nodered.org/t/change-value-on-file/42273/3 "2021-03-08T16:03:42Z")

</div>

Hello, thank for your fast answer.  
Below the file in the native program.  
 ![3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/998427d7f3010b752e5a5984e0ccfdba8cb081c0.png)

But the node red do not identify this mode, then I think in search the "name", I do not know if is the best way.  
I need read and use the "value" on node red.

Ps. this file is just to read and do not have problem on main program.

---

<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:** [8 March 2021 17:40 UTC](https://discourse.nodered.org/t/change-value-on-file/42273/4 "2021-03-08T17:40:28Z")

</div>

My approach would be to split the file into separate lines, and then split those lines into variables, using a Javascript function like this:

```auto
var obj = {};
var exp = /^GLOBAL (INT|BOOL|STRING) (\w+)=(.*$)/gm;

while (grp = exp.exec(dat)) {
    var [grp, typ, key, val] = grp;
    obj[key] = typ == 'INT' ? +val : typ == 'BOOL' ? val == 'TRUE' : val;
}

msg.payload = obj;
return msg;

```

This results in a payload that contains ALL the global variables, so you can choose which ones to use in your downstream flows:

```auto
{
  "VELOCIDADE": 10,
  "COD_PROD": 1,
  "PARKING": true,
  "MACHINE_A": true
}

```

---

<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:** [8 March 2021 17:43 UTC](https://discourse.nodered.org/t/change-value-on-file/42273/5 "2021-03-08T17:43:57Z")

</div>

Incidentally, this same transformation can be accomplished by using a `change` node, with this JSONata expression:

```auto
payload.$split('\n').$match(/^GLOBAL (INT|BOOL|STRING) (\w+)=(.*$)/) {
    groups[1]: groups[0] = 'INT' ? $number(groups[2]) :
                groups[0] = 'BOOL' ? $boolean(groups[2]) :
                groups[2]
}

```

which I actually find a bit cleaner to read, but hey that's just me...

---

<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:** [7 May 2021 17:44 UTC](https://discourse.nodered.org/t/change-value-on-file/42273/6 "2021-05-07T17:44:07Z")

</div>

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