const fs = require('fs')
const readline = require('readline')
var fp = 'e:/Test/watch/control.txt'
var fml = 'e:/Test/watch/control2.txt'
var file = fs.readFileSync(fp)
var cnt = 0
// console.log('Initial File content: \n' + file)
// 0o111 is read only flag for windows || 0o222 Write flag
// Used to lock a given file to prevent alterations in the middle of processing the file.
function rwlock(file, rw) {
if (rw === 'r') { rw = 0o111 }
if (rw === 'w') { rw = 0o222 }
fs.chmod (file, rw, (err) => {
if (err) throw err
// code for successful change.
})
}
// appeand file puts the data at the end of file, \n is the 'end of line' character.
function filerb(file, line) {
fs.appendFile(file, line + '\n', (err) => {
if (err) throw err
})
}
// inputfile = file || linne = The line to be modified in the duplicate file.
// reads file & creates a duplicate file, with a modified line using the count of the lines as a control method
// Can execute code on reading the file, keywords & careful planning of control files.
function ProcessFile(inputfile, linne) {
ins = fs.createReadStream(inputfile)
outs = new (require('stream'))()
rl = readline.createInterface(ins, outs)
cnt = 0
rl.on('line', (line) => {
cnt = cnt+1
if (cnt === linne || cnt === linne+2) {
console.log(line + ' - ' + cnt)
line = line + ' - Modified - Line count > ' + cnt
// filerb(fml, line)
} else {
// filerb(fml, line)
}
})
rl.on('close', (line)=> {
console.log('done reading file.')
})
}
fs.watchFile(fp, { persistent: true, interval: 100 }, ()=> {
console.log('File changed .. ')
file = fs.readFileSync(fp)
console.log('File content at : ' + new Date())
// Space for checking for file changes on a watched file. executes when file 'changes' including saves/syncs
// ProcessFile(fp, 2)
})
// WIP *DePhoegon
Comments
0 B
|👍
/👎