1

This is what I have so far but it returns only one proxy because it rewrites over it x (however many proxies) times. I do not want to make a new file but instead rewrite proxies.txt with every proxy.

const fs = require("fs");

const formatProxies = () => {
  const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
  const split = rawProxies.trim().split("\n");

  for (const p of split) {
    const parts = p.trim().split(":");
    const [ip, port, user, pass] = parts;
    fs.writeFileSync(
      "./proxies.txt",
      user + ":" + pass + "@" + ip + ":" + port + "\r\n",
      { encoding: "utf8" }
    );
  }
};
formatProxies();

2 Answers2

1

Does this work?

const fs = require("fs");

const formatProxies = () => {
  const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
  const split = rawProxies.trim().split("\n");

  const lines = []
  for (const p of split) {
    const parts = p.trim().split(":");
    const [ip, port, user, pass] = parts;
    lines.push(user + ":" + pass + "@" + ip + ":" + port)
  }
  fs.writeFileSync(
    "./proxies.txt",
    lines.join("\r\n"),
    { encoding: "utf8" }
  );
};
formatProxies();
twharmon
  • 4,153
  • 5
  • 22
  • 48
0

Node.js has fs.appendFileSync, which writes to the end of the file instead of overwriting the whole thing.

  • If you want to empty the file before you write the entries to it, you could use fs.truncateSync; but that's a problem, because if your script stops somehow between emptying the file and appending the results, you've lost the file. twharmon's solution works well. – Jack Robinson Apr 14 '20 at 21:03