-1

Im creating scripts in WinCC unified where javascript is used, i have some code which finds specific 3 lines in a text file and deletes the values there, it works but i wish to also basicaly delete the whole line, as if you were to press backspace when there's no text on a text line.

The code

HMIRuntime.FileSystem.ReadFile(path, "utf8").then(
function(text) {
const lines = text.split('\n');
delete lines[noteNumber];
delete lines[noteNumber+1];
delete lines[noteNumber+2];
//HMIRuntime.Trace("lines:" +lines.join('\n')); 
FileSystem.WriteFile(path, lines.join('\n'),"utf8" )
});  

Thank you for any nice sould to help out.

I have tried the splicing but now the part which reads the .txt file and writes its data into HMI arrays is not updating and the values are no longer being read. The code which reads the .txt file is here

HMIRuntime.FileSystem.ReadFile(path, "utf8").then(
function(text) {

 for (let i = 0; i < maxNoteNumber; i++) {
 HMIRuntime.Trace("Trace Message"+ text.split('\n',i));
 Tags('strDate[' + i + ']').Write(text.split('\n')[i*3]);
 Tags('strName[' + i + ']').Write(text.split('\n')[i*3+1]);
 Tags('strNote[' + i + ']').Write(text.split('\n')[i*3+2]);
 }
 }).catch(function(errorCode) {
 HMIRuntime.Trace("read error:" + errorCode)
 for (let i = 0; i <= maxNoteNumber; i++) {
 Tags('strNote[' + i + ']').Write('\n')//emty overwrite
  }
  //create a emtpy file 
  HMIRuntime.FileSystem.WriteFile(path," ", 'utf8').then(
  function() {
    HMIRuntime.Trace('Write file finished sucessfully');
    }).catch(function(errorCode) {
     HMIRuntime.Trace('Write failed errorcode=' + errorCode);
});
});
Pavel
  • 1
  • 1

1 Answers1

0

deleteing a property from an array will not stop that line from being used when .joining:

console.log(
  [1, , 3].join('\n')
);

const arr = [1, 2, 3];
delete arr[1];
console.log(
  arr.join('\n')
);

Splice the lines you want to remove out instead:

lines.splice(noteNumber, 3);

and then lines.join('\n') will give you what you want.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Great thank you. I tried it and while its correctly doing what i want in the text file the part which reads the text file and saves that data into HMI tag arrays now is broken and doesnt update when i delete a text file. Sorry im new to stackoverflow so i dont know how to send you the code for you to see how i read the text file. Ill just reply to myself i guess since i dont have enough characters in this comment – Pavel Mar 22 '21 at 17:09
  • Since you removed 3 lines, it sounds like you might want to decrement `maxNoteNumber` by 3 before reading the file again? `maxNoteNumber -= 3`? – CertainPerformance Mar 22 '21 at 17:18
  • It reads the file correctly actually. But it bugs out when i dont have a "empty space" on the position of where its being read. So when i opened the text document and just held enter, then saved and reloaded my program it works correctly. But this is really weird because i didnt have this problem before. – Pavel Mar 22 '21 at 17:23
  • What precisely do you mean, "bugs out"? What happened, and what did you expect to happen? – CertainPerformance Mar 22 '21 at 17:24
  • Well basically the code works like this: Every 3 lines it reads the data into 3 differrent arrays for a date, name and note. Like this line 0. - date[0], line 1 - name[0], line 2 - note[0], line 3 - date[1], line 4 - name[1] ,... So i would have 3 large arrays of data which would read from its "designated" spot and if there was no text on that line, it would give back a empty string which is what i wanted. Now when i use the splice to remove text i expected every array value to move its position back by 1. – Pavel Mar 22 '21 at 17:37
  • What is actually happening though is that it moves into the value i have deleted back, but all of the array values with a higher [number] stay the same. As if before it could read the empty lines and return a empty string, now it doesnt overwrite the value which was previously there with an empty string. – Pavel Mar 22 '21 at 17:37
  • That doesn't make any sense. If you have an array `[1, 2, 3]` then splice one item out `arr.splice(1, 1)`, you get `[1, 3]`. The items in later positions shift down. The later positions don't stay static - you don't get `[1, 3, 3]`. – CertainPerformance Mar 22 '21 at 17:50
  • If you have multiple arrays whose indicies correspond to each other, splice them all, not just the `lines` array in the function. – CertainPerformance Mar 22 '21 at 17:50
  • But i dont think the problem is in the splicing, it does it correctly in the .txt file but the part which reads the file is not updating. I have updated my question with the code that reads the .txt file. And as i previously said. It works when i open the .txt file and hold enter to create empty lines, but its not like that is a viable way to leave it be. – Pavel Mar 22 '21 at 17:55
  • It's unclear what the problem is. If you have other arrays whose indicies correspond to the indicies in `lines`, you'll need to splice them too. This is starting to sound like a [chameleon question](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions). – CertainPerformance Mar 22 '21 at 18:01
  • It looks like this is precisely a chameleon question lol. And yeah this is my first time programming in javascript since im a PLC programmer. Eitherway its fine if you dont feel like fixing my code. And the way my program is set up is extremely stupid in itself due to limitations in the system im programming in. – Pavel Mar 22 '21 at 19:17