Node Js: Test To See If A File Is Locked For Editing By Another Process
I am writing some code in NodeJs, and want to check to see if the file is in use by another process, if it is then do nothing, if it isn't in use do something. fs.stats is kind of
Solution 1:
Code I ended up using after some instruction from the comments left.
var delInterval = setInterval(del(), 1000);
function del(){
fs.open(filePath, 'r+', function(err, fd){
if (err && err.code === 'EBUSY'){
//do nothing till next loop
} else if (err && err.code === 'ENOENT'){
console.log(filePath, 'deleted');
clearInterval(delInterval);
} else {
fs.close(fd, function(){
fs.unlink(filePath, function(err){
if(err){
} else {
console.log(filePath, 'deleted');
clearInterval(delInterval);
}
});
});
}
});
}
Post a Comment for "Node Js: Test To See If A File Is Locked For Editing By Another Process"