Copy file to new location - javascript

I'm trying to copy a file with the data inside a new file in a folder.
I tried doing this but it didn't work:
copyFile(`./data/guilddata/guilds/default/GUILDID.json`, `./data/guilddata/guilds/${guild.id}/GUILDID.json`, (err) => {
if (err) throw err;
});
https://www.npmjs.com/package/fs-copy-file
Does anyone know what to do? (${guild.id} just means the guild id, the folder is already there). I also get no errors. Thank you

const fs = require('fs');
fs.copyFile('./data/guilddata/guilds/default/GUILDID.json', './data/guilddata/guilds/' + guild.id + '/GUILDID.json', (err) => {
if (err) throw err;
console.log('All done! The file is copied!');
});

Related

How to retrieve a file using node.js fs readFile function without specifying the name?

I'm currently stuck trying to retrieve a file from file system in order to send it through api to the client. For my backend I'm using express js
I'm using fs library and currently I'm trying to do it with readFile function, but I want to do it without specifying the file name or just the file extension because it will depend from file file will be uploaded from client.
What I tried until now (unsuccessfully) is shown below:
router.get("/info/pic", async (req, res) => {
const file = await fs.readFile("./images/profile/me.*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/me.*'
return;
}
console.log(data);
});
});
const file = await fs.readFile("./images/profile/*.*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/*.*'
return;
}
console.log(data);
});
const file = await fs.readFile("./images/profile/*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/*'
return;
}
console.log(data);
});
If I specify the file name everything works fine, like: fs.readFile("./images/profile/me.jpg". but as I said, I don't know for sure the right extension of that file.
Important info: In that directory there will be only one file!
Please help me!
Thank you in advance!
If there is only one file in the directory, the following loop will have only one iteration:
for await (const file of fs.opendirSync("./images/profile")) {
var image = fs.readFileSync("./images/profile/" + file.name);
...
}
const fs = require('fs');
fs.readdir('./images/profile', function (err, files) {
//handling error
if (err) {
return console.log('err);
}
files.forEach(function (file) {
// Do whatever you want to do with the file
});
});

fs Error: EISDIR: illegal operation on a directory, read

I'm getting "Error: EISDIR: illegal operation on a directory, read" after trying to read the content of a .json. This is how I'm trying to access the file. I'm using the FileSystem of node js.
fs.readFile( path, ( err, fileData) => {
if (err) {
throw err;
}
else {
return fileData;
}
});
While debugging I can see that the error is thrown before the if statement.
Any idea?
Maybe the path to the file is not the right one, make sure the path of your file looks like the one that appears in the following code:
const fs = require('fs');
fs.readFile('PATH_TO_YOUR_FILE/File_Name.json', (err, fileData) => {
if (err) {
throw err;
} else {
console.log(JSON.parse(fileData));
}
});

How do I overwrite a file with the fs module?

I'm making an electron app for windows. When I try to overwrite a txt file it just deletes everything. I want that but the content I am trying to put is not writing to the file.
Here is the code for when receive a prompt to save:
ipcRenderer.on('saveFile', function(){
console.log(fileData.path)
fs.writeFile(fileData.path, editor.innerText, 'utf8', (err) => {
if(err) throw err;
console.log('File Saved')
})
})
Here is the code when I send the file data (file path, text in file, etc.):
fileBtn.addEventListener('change', function(e){
if(fileBtn.files[0].type.substring(0, 4) == 'text'){
file.ext = fileBtn.files[0].name.substr(fileBtn.files[0].name.indexOf('.'))
console.log(fileBtn.files[0])
file.path = fileBtn.files[0].path
file.text = fs.readFileSync(file.path, 'utf8');
ipcRenderer.send('newFile', file)
}else{
console.error('Not a valid file type')
return;
}
})

Read the lines of several txt files that are inside a folder and create a single text file containing all lines of text

Read the lines of several txt files that are inside a folder and create a single text file containing all lines of text. Here is an example that I make work but read only a specific file, and then i create one with the lines of text contained in that file.
const testFolder = './txt_files/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
})
fs.readFile('txt_files/url-list1.txt', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
fs.writeFile('txt_files/test.txt', data, function(err) {
if(err) {
return console.log(err);
}
console.log("El archivo con todas las url se guardó!");
});
});
This example works. It reads a single file from the 'txt_files' folder, and creates a new txt with the lines of text it extracted. What I want to do is read all the files in that folder, and create a new one with all lines of text.
Please help me!
const testFolder = './txt_files/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
fs.readFile(testFolder+file, 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
fs.appendFile('txt_files/test.txt', data+"\n", function(err) {
if(err) {
return console.log(err);
}
console.log("El archivo con todas las url se guardó!");
});
});
});
});
So, using what you already provided, this is what I came up with. FS has a function called appendFile which creates the file if it does not exist and then appends the data to the file's EOF.
Read more on the function here
I added a check for TXT-extension so this only appends data from actual text files.
const testFolder = './txt_files/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
// only match TXT Files
if (file.substring(file.length - 4, file.length) == ".txt"){
fs.readfile(testFolder+file, 'utf8', (err, data) => {
fs.appendFile(testFolder+'allfiles.txt', data+"\n", function (err) {
if (err) throw err;
});
});
}
});
});

Error while inserting file to MongoDB from Node.js

I am trying to connect to mongoDB from node.js and upload a file("functions") to MongoDB.
Can someone please verify whats the issue with my code is.
When I run the js file, I am getting following error:
Error: Cannot find module 'mongodb'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
Code is as follows:
var mongodb = require('mongodb');
var url = require('url');
var log = console.log;
var currentTimeStamp = new Date();
var file = require (__dirname + '/functions');
mongodb.MongoClient.connect('mongodb://phx8b03c-fb1d-6.stratus.phx.ebay.com,phx8b03c-316d-6.stratus.phx.ebay.com,phx8b03c-9564-6.stratus.phx.ebay.com',
function (err, client) {
if (err) throw err;
client.createCollection('lbTopology' , function (err, collection) {
if (err) throw err;
collection.insert(file, 'lbTopology' , function (err) {
if (err) throw err;
client.close(function (err) {
if (err) throw err;
console.log('done');
});
});
});
});
Can someone please let me know what the issue is? Thanks a lot in advance
It looks like you don't have mongodb installed. Did you npm install mongodb in the same directory with your code or do you have a node_modules folder with mongodb in it?

Categories

Resources