I need to create file from directory like following
application/userxxx.txt/manifest.txt
The path is constant except the userxxx which can be any alpha numeric
/application/
user12.txt
newfile.txt
newFile2.txt
There is only one file which start with user...
I think of using the which is currently not working..
fs.readdir('c://application', function (err, files) {
if (err) {
throw err;
}
and then get all the files under the application
and search for file which start with userabcd1234.txt and when I find it do the read file like following
readFile('application/userabcd1234/manifest.txt')
There is no two files inside application which start with /user. just one but after the user. and before the third '/manifest.txt' can be any random alpha numeric.
You can do something like
var filePath = path.join(__dirname, '../your path to application folder');
fs.readdir(filePath, function (err, files) {
if (err) {
return console.error(err);
}
files.forEach(function (file) {
if (file.indexOf('user') === 0) {
var relFilePath = filePath + '/' + file + '/manifest.txt';
fs.readFile(relFilePath,'utf8', function read(err, data) {
if (err) {
throw err;
}
console.log(data);
});
}
});
});
Related
I have to run certain gulp task over multiple json files in a folder but the task would fetch files different location based on the filename.
I am able to run the task by passing the filename as an argument in cmd but I want to automate the script so that it would get executed for all the files in the src location.
gulp.task("writeJSON", function() {
dataObj = require("./src/data/" + argv["filename"] + ".json");
dataObjKeysList = require("./src/data/stats/" + argv["filename"] + ".json");
segregateData(dataObj, dataObjKeysList, tabspace, false);
gulp
.src("./src/metadata.html")
.pipe(rename(argv["filename"] + ".html"))
.pipe(gulp.dest("./src/output"));
});
Any help would be greatly appreciated.
I am able to resolve the above issue using node filestream. I found this useful article
Filewalker Source
Used the below utility function which take the directory path and callback as args.
function filewalker(dir, done) {
let results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file){
file = path.resolve(dir, file);
fs.stat(file, function(err, stat){
// If directory, execute a recursive call
if (stat && stat.isDirectory()) {
// Add directory to array [comment if you need to remove the directories from the array]
results.push(file);
filewalker(file, function(err, res){
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
Added the below execution in my gulp task
filewalker("./src/data/stats/" , function(err, dataFilesList){
if(err){
throw err;
}
dataFilesList.map((name) => {
let fileName = path.basename(name);
fileName = fileName.split('.')[0];
gutil.log('Generating ' + fileName + '.html file.');
});
});
I have a folder called 'Received' and two more folders called 'Successful' and 'Error'.
All new files will be stored in the 'Received' Folder and upon being stored in the said folder, it will be processed by my system. Successfuly parsed files will be moved to the 'Successful' folder, while all files that had issues will be stored in the 'Error' folder.
My main concern is basically moving files between directories.
I have tried this:
// oldPath = Received Folder
// sucsPath = Successful Folder
// failPath = Error Folder
// Checks if Successful or fail. 1 = Success; 0 = Fail
if(STATUS == '1') { // 1 = Success;
fs.rename(oldPath, sucsPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(sucsPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close', function () {
fs.unlink(oldPath, callback);
});
readStream.pipe(writeStream);
}
else {
callback(err);
}
return;
}
callback();
});
}
else { // 0 = Fail
fs.rename(oldPath, failPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(failPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close', function () {
fs.unlink(oldPath, callback);
});
readStream.pipe(writeStream);
}
else {
callback(err);
}
return;
}
callback();
});
}
But my concern here is that it deletes the original folder and passes all files into the specified folder. I believe the logic in the code is that, it literally renames the file (directory included). I also came across 'await moveFile' but it basically does the same thing.
I just want to move files between directories by simply specifying the file name, the origin of the file, and its destination.
As mentioned by rufus1530, I used this:
fs.createReadStream(origin).pipe(fs.createWriteStream(destination));
Deleting the file would be the next step.
I used this:
fs.unlinkSync(file);
Starting with 8.5 you have fs.copyFile which the easiest way to copy files.
So you would create your own move function, that would first try a rename and then a copy.
const util = require('util')
const copyFile = util.promisify(fs.copyFile)
const rename = util.promisify(fs.rename)
const unlink = util.promisify(fs.unlink)
const path = require('path')
async function moveFileTo(file, dest) {
// get the file name of the path
const fileName = path.basename(file)
// combine the path of destination directory and the filename
const destPath = path.join(dest, fileName)
try {
await fs.rename(file, destPath)
} catch( err ) {
if (err.code === 'EXDEV') {
// we need to copy if the destination is on another parition
await copyFile(file, destPath)
// delete the old file if copying was successful
await unlink(file)
} else {
// re throw the error if it is another error
throw err
}
}
}
Then you could use it that way await moveFileTo('/path/to/file.txt', '/new/path') which will move /path/to/file.txt to /new/path/file.txt.
The purpose is to extract the contents of a .sketch file.
I have a file with the name myfile.sketch. On renaming the file extension to myfile.zip and extracting the same in Finder, I'm able view the files within. I tried doing the same on the server using Node.js by renaming the file extension to .zip. I wasn't able to extract the files, rather I got some ZIP files within the files.
var oldPath = __dirname+'/uploads/myfile.sketch',
newPath = __dirname+'/uploads/myfile.zip';
fs.rename(oldPath, newPath, function (err) {
console.log('rename callback ', err);
});
Is it possible to extract a non-ZIP file using frameworks like JSzip?
As a .sketch file is essentially a ZIP file, the extension does not matter. Any tool that is capable of unpacking a ZIP file will work.
You can verify this with the file command:
$ file myfile.sketch
myfile.sketch: Zip archive data, at least v1.0 to extract
As you are working on the server already, there is nothing stopping you from just using the OS's command line tools like unzip.
Like this:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function unzip() {
const filename = 'myfile.sketch'
const { stdout, stderr } = await exec('unzip ' + filename);
console.log('stdout:', stdout);
console.log('stderr:', stderr);
}
unzip();
Doing it with JSZip is straight-forward as well:
var fs = require('fs');
var JSZip = require('jszip');
new JSZip.external.Promise(function (resolve, reject) {
fs.readFile('myfile.sketch', function(err, data) {
if (err) {
reject(e);
} else {
resolve(data);
}
});
}).then(function (data) {
return JSZip.loadAsync(data);
})
This is a Node application, running express server. I have a folder with text files in it. I need to be able to go into each one of those files inside the folder, and extract lines that include a word "SAVE".
I am stuck at this step.
app.get('/logJson', function (req, res) {
const logsFolder = 'C:/logs/';
fs.readdir(logsFolder, (err, files) => {
if (err) {
res.send("[empty]");
return;
}
files.forEach(function(filename){
var logFiles = fs.readFileSync (logsFolder + filename, 'ascii').toString().split("\n");
console.log(logFiles + "\n");
})
})
})
I cannot figure out where do I include this:
var log = (logFiles.split(/.*SAVE.*/g)||['no result found'])[0];
Any help would be appreciated
If you only want to print out all lines containing the word SAVE, you could do it like this.
Remark: I did not run this code.
app.get('/logJson', function (req, res) {
const logsFolder = 'C:/logs/';
fs.readdir(logsFolder, (err, files) => {
if (err) {
res.send("[empty]");
return;
}
var lines = [];
files.forEach(function(filename) {
var logFileLines = fs.readFileSync (logsFolder + filename, 'ascii').toString().split("\n");
// go through the list of logFileLines
logFileLines.forEach(function(logFileLine) {
// if the current line matches SAVE, it will be stored in the variable lines
if(logFileLine.match(/SAVE/)) {
lines.push(logFileLine);
}
})
})
// the variable lines is printed to the console
console.log(lines);
})
})
I am using the node-dir package (https://www.npmjs.org/package/node-dir) to list files recursively from a path but I cannot succeed to add a filter to my result.
For instance, I want in my result array only files with 'mp3' extension.
Does anyone know how I can do that?
dir.files(__dirname, function(err, files.filter(ismp3file)) {
if (err) throw err;
console.log(files);
});
function ismp3file(elmt){return element.substr((~-element.lastIndexOf(".") >>> 0) + 2) === "mp3";}
i tried to add filter, but i got an error.
Thanks for helping
Try this:
var dir = require('node-dir');
function isMp3File(file) {
return (file.indexOf(".mp3") > -1);
}
dir.files(__dirname, function(err, files) {
if (err) throw err;
files = files.filter(isMp3File);
console.log(files);
});
Or if you prefer to verify the file ends with .mp3, you could add string.js as a dependency:
var dir = require('node-dir'),
S = require('string');
function isMp3File(file) {
return (S(file).endsWith('.mp3'));
}
dir.files(__dirname, function(err, files) {
if (err) throw err;
files = files.filter(isMp3File);
console.log(files);
});