Reputation: 105
I'm currently trying to baffle my head through this - so the scenario is:
1. Download textFile1 from FTP
2. Download textFile2 from FTP
3. Read data and compare in textFile1&2 from local files on PC
4. Save differences (like full lines) only to textFile 3
I've tried doing fs.readFile on it - and compare fileDiff (npm diff); however i am a bit unsure on how to handle so it only saves the actual difference between 1&2 - to a third text file.
See it as a "compare & save"
Hope someone have any ideas on how to achieve this, its a bit hard to explain.
// Update
Code snippet (Or look below): https://jsfiddle.net/menix/py3eqnfL/
const ServerLog = "ServerDM/TheIsle/Saved/Logs/TheIsle.log";
const LocalSaveOld = "TheIsleData/TheIsle.log";
const LocalSaveNew = "TheIsleData/TheIsleNew.log";
async function example() {
const client = new ftp.Client();
client.ftp.verbose = true;
try {
await client.access({
host: config.ftpHost,
port: config.ftpPort,
user: config.ftpUser,
password: config.ftpPassword,
secure: config.ftpSecure,
});
await client.downloadTo(LocalSaveOld, ServerLog);
if (fs.existsSync(LocalSaveOld)) {
fs.readFile(LocalSaveOld, function (err, data1) {
console.log(data1);
fs.readFile(LocalSaveNew, function (err, data2) {
console.log(data2);
var difference = fileDiff.diffLines(data1, data2);
console.log(difference);
});
});
await fs.rename(LocalSaveOld, LocalSaveNew, function (err) {
console.log("old was copied to new");
if (err) console.log("ERROR: " + err);
});
}
} catch (error) {
console.log("FTP ERROR");
client.close();
}
}
This the code i currently have played with, it most likely needs recode obviously.
What my thoughts is first it downloads the ServerLog file through FTP to PC (It saves it as LocalSaveOld on pc) - then it should redownload ServerLog again after 5 minutes, save it as LocalSaveNew
Then i wanna compare the LocalSaveOld with the LocalSaveNew - and have the "new data" in the 5 minute period saved to a third file (This is what i am really lost on)
I figured out how to download ServerLog from FTP, save it as LocalSaveOld.
I figured out how to rename the LocalSaveOld to LocalSaveNew
Upvotes: 0
Views: 3949
Reputation: 105
const string1 = fs.readFileSync(LocalSaveOld, { encoding: "utf8" });
const string2 = fs.readFileSync(LocalSaveNew, { encoding: "utf8" });
const diff = (diffMe, diffBy) => diffMe.split(diffBy).join("");
const finalString = diff(string2, string1);
fs.writeFile("./TheIsleData/diff.log", finalString, function (err) {
if (err) return console.log(err);
console.log("written");
Was the answer to compare - and i have it fully working now!
Upvotes: 2