Unlink files inside fs.watch in node - javascript

Is there a way or package that allows me to watch for new files and delete them?
Even if i try to delete files in sub directories. i get errors because the file is locked.
fs.watch(example, function (eventType, filename) {
fs.unlink("example/"+filename+"/testfile", (err) => {
if (err) throw err;
console.log('successfully deleted');
});
});
Appreciate your time

Related

How can I edit file in JavaScript?

I want to be able to:
-edit the data of a .dat file on my computer for a website.
-pull data from the file to use it later on.
I know a tiny bit about javascript and heard javascript cannot directly edit databases.
Is a .dat file in my computer a database?
I have done a few things in Javascript for websites but I haven't done anything complicated completely myself. I created some websites before and I have a basic understanding of HTML and CSS.
Please phrase your response as simply as possible. Explain the meaning of any complicated but necessary terms.
You need some server-side script to access the filesystem of the server such as PHP or NodeJs...
Nodejs example here.
let fs = require('fs');
Appeding file:
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
Delete file:
fs.unlink('mynewfile2.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});
Read file :
fs.readFile('demofile1.html', function(err, data) {
if (err) throw err;
console.log(data);
});

How to generate a .txt file with form data and download it with Node JS?

The system has a form, on the side of the backend I recover that data, what I can't do is download that data in a .txt file.
I was using fs.writefile (), but when the system is uploaded in the cloud it does not access the destination folder.
var new_ingreso = new Ingreso(req.body);
//I want to download the data from req.body in a file.txt
fs.writeFile(
'nameFile.txt',
new_ingreso.nameUser,
error => {
if (error)
console.log(error, 'el archivo no fue creado');
else
console.log('El archivo fue creado');
});
}
The file is created without problems what I want is to know if there is any way that this file can be downloaded or if there is another way to download.
He's seeing a way but I'm not sure how to continue.
var file = fs.writeFile(....);
You need to create a route like this. You need to do something with the file path to find your file. No security, code not tested, but it gives you the idea of how to do it.
See https://expressjs.com/en/api.html#res.sendFile
get('file', (req, res) => {
res.sendFile('nameFile.txt', options, function (err) {
if (err) {
next(err)
} else {
console.log('Sent:', 'nameFile.txt')
}
})
})

fs.unlink() does not work and callback does not fire

It was working a while ago, and I'm not sure what changed to where the fs.unlink() function seems to be totally skipped. The file for deletion is saved in the root package directory, while the typescript file that contains my code is in the dist folder. It worked when I passed in only the fileName like this: fs.unlink(fileName). But now that doesn't work, and it also doesn't it work when I explicitly direct it to the root folder by using either fs.unlink("./" + fileName) or fs.unlink("../" + fileName), or even when I hardcode the path!
Below is my code:
s3.putObject(params, function(err, data){
// ...
console.log("Data uploaded to S3 bucket");
fs.unlink("./" + fileName, function(err){
if(err) {
return console.log("Delete error: " + err);
}
else{
console.log("file deleted successfully");
}
});
console.log("Before process.exit()");
process.exit();
});
When run, this code logs:
Data uploaded to S3 bucket
Before process.exit()
So as you can see, it skips both the if and else statements on fs.unlink(), and doesn't delete the file. It's acting like that block of code doesn't exist at all and I can't figure out why.
You are using async unlink function that's why it is executing next statement. Use
unlinkSync instead.

Can fs.unlink() delete a empty or non empty folder?

I am new to Node.js.
const fs = require('fs');
fs.unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
This is some code that I copied from a node.js document file system intro example.
But, I am confused. Can unlink() delete a folder or not?
I have tried but it doesn't work.
So, can unlink() delete a folder or not?
The fs.unlink(path, callback) function is used to delete a file not a folder.
To remove a folder you can use the fs.rmdir(path, callback) function instead.

Files is deleting before its used in node js

I'm new to node js and i'm trying to do the following:
function createPasswordfile(content)
{
fs.writeFile(passwordFileName,content, function(err) {
if(err) {
console.log("Failed on creating the file " + err)
}
});
fs.chmodSync(passwordFileName, '400');
}
function deletePasswordFile()
{
fs.chmodSync(passwordFileName, '777');
fs.unlink(passwordFileName,function (err) {
if (err) throw err;
console.log('successfully deleted');
});
}
and there are three statements which call these functions:
createPasswordfile(password)
someOtherFunction() //which needs the created password file
deletePasswordFile()
The problem I'm facing is when I add the deletePasswordFile() method call, I get error like this:
Failed on creating the file Error: EACCES, open 'password.txt'
successfully deleted
Since its non blocking, I guess the deletePasswordFile function deletes the file before other function make use of it.
If deletePasswordFile is commented out, things are working fine.
How should I prevent this?
writeFile is asynchronous, so it's possible the file is still being written when you try and delete it.
Try changing to writeFileSync.
fs.writeFileSync(passwordFileName, content);

Categories

Resources