0

I am trying to edit a plain text file in javascript, that is in the same directory as the .js file. This is what I have right now:

var fs = require('file-system');
var userid = 247927078954074114;
fs.writeFile("./muted.txt", `${userid}\n`);

That works perfectly using the filesystem api, but what I want to do is edit the file and delete a specific user id within it. There are a bunch of user ids in the text file, and now that I can add them, I am trying to figure out how to delete one.

John Landon
  • 345
  • 1
  • 3
  • 10

1 Answers1

-1
  1. Read the file into a buffer variable.
  2. Search and delete the entry. A simple way of doing that would be to split the buffer into an array of strings (the separator here seems to be a newline character), iterate over the array to find and remove the corresponding entry and join back the array's strings together into your buffer. Alternatively, you could be using a regular expression to remove the entry directly in the buffer but it's less robust / more error prone.
  3. Write the buffer back to the file.
solidgumby
  • 2,594
  • 1
  • 16
  • 9