I am new to Node.js and I am looking for guidance on how to compile together different .md files.
I downloaded this repository and would like to join/merge/compile-together all .md files. So I would like to get one single file with the content of each of the individual files so that I can have a new .md file with all the content inside.
This is what I was thinking about...
var docsdir = '../Docs/';
var filesArray = fs.readdir(docsdir, function(err,files){
if (err) console.error(err);
// missing how to check if file is a directory
return files;
}
var newFile;
for(var i = 0; i < files.length; i++){
fs.readFile(files[i], function read(err, data) {
if (err) {
throw err;
}
newFile = newFile + data;
});
}
fs.writeFile('wholeDocs', newFile)
THe directory structure is in this example:
+
|-Docs
|
|-folder1
| |
| |-file1.md
|
|
|-folder2
| |
| |-file3.md
etc...
This is offline processing so you can use fs.Sync() to do the job that easier than async function.
Note that async is a must if you write web app, async help avoid app blocking.
You also need to known about recursive.
You can learn from code below:
var fs = require('fs'), path = require('path');
function findAllFile(dir, ext) {
var ret = [];
function findOneLevel(dir) {
fs.readdirSync(dir).forEach(function(file) {
var name = path.join(dir, file);
var stat = fs.statSync(name);
if (stat.isDirectory()) {
findOneLevel(name);
} else if (stat.isFile() && path.extname(name).toLowerCase() === ext) {
ret.push(name);
}
});
}
findOneLevel(dir);
return ret;
}
function concatAllMd(dir, outFile) {
var contents = [];
findAllFile(dir, '.md').forEach(function(file) {
contents.push(fs.readFileSync(file, 'utf8'));
})
fs.writeFileSync(outFile, contents.join('\n'));
}
// use
concatAllMd('Docs', 'output.md');
Related
Hello I am trying to find first file with given filename ( piece of filename ).
It works fine but it take a while to take result
There is code
const fs = require("fs");
const dirCheckIn =
"\\\\192.168.2.4\\Photos";
exports.checkUploadedFiles = (req, res) => {
let fileName = req.params.filename;
const getAllFiles = function (dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push(file);
}
});
return arrayOfFiles;
};
const uploadedFiles = getAllFiles(inventDirCheckIn);
console.log(uploadedFiles)
let result = uploadedFiles.find(
(result) => result.startsWith(fileName));
if (!result) {
res.send('nothing found')
} else if (result) {
res.send(result)
}
}
It works fine but for example if I have over 7000 photos it takes about 5 sec to get result.
Maybe there is smarter solution?
How can I make it in better way? I want to check if file is uploaded into dir Photos.
I got simple api route /api/getUploadedFiles/:filename
Also I want use startsWith because sometimes I do not know full name of file
/**
*
* #param filePath path to file which is to be checked if it exists.
*/
private checkFileExistsSync(filePath: string) {
let flag = true;
try {
fs.accessSync(filePath, fs.constants.F_OK);
} catch (e) {
flag = false;
}
return flag;
}
// Example usage
// path to the file
const dirCheckIn =
"\\\\192.168.2.4\\Photos";
if (checkFileExistsSync(dirCheckIn)) {
// if the file exists do something...
}
if (!checkFileExistsSync(dirCheckIn)) {
// if the file doesn't exists do something...
}
The code listed below searches for files that contains a specified string under it's directory/subdirectories.
to activate it, you type node [jsfilename] [folder] [ext] [term]
i would like to change it so it will search without the base folder, i don't want to type ./ , just node [jsfilename] [ext] [term]
so it already know to search from it's location.
i know it has something to do with the process.argv but it need a hint what should i do.
PS:.
I already tried to change the last raw to :
searchFilesInDirectory(__dirname, process.argv[3], process.argv[2]);
it giving me noting...
const path = require('path');
const fs = require('fs');
function searchFilesInDirectory(dir, filter, ext) {
if (!fs.existsSync(dir)) {
console.log(`Welcome! to start, type node search [location] [ext] [word]`);
console.log(`For example: node search ./ .txt myterm`);
return;
}
const files = fs.readdirSync(dir);
const found = getFilesInDirectory(dir, ext);
let printed = false
found.forEach(file => {
const fileContent = fs.readFileSync(file);
const regex = new RegExp('\\b' + filter + '\\b');
if (regex.test(fileContent)) {
console.log(`Your word has found in file: ${file}`);
}
if (!printed && !regex.test(fileContent)) {
console.log(`Sorry, Noting found`);
printed = true;
}
});
}
function getFilesInDirectory(dir, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
let files = [];
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.lstatSync(filePath);
if (stat.isDirectory()) {
const nestedFiles = getFilesInDirectory(filePath, ext);
files = files.concat(nestedFiles);
} else {
if (path.extname(file) === ext) {
files.push(filePath);
}
}
});
return files;
}
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
If I get what are you trying to achieve. You can do so by slightly changing your function call in the last line.
Change
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
to
searchFilesInDirectory(process.cwd(), process.argv[3], process.argv[2]);
Edit
As #Keith said in comments use process.cwd() to get the current working directory instead of __dirname
If you want it to work for both conditions then you need to do a conditional check...
if(process.argv.length === 5){
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
}else if(process.argv.length === 4){
searchFilesInDirectory(process.cwd(), process.argv[3], process.argv[2]);
}else{
throw new Error("Not enough arguments provided..");
}
I want to delete an empty/non-empty directory from my local system and i have restriction that i need to do that only using javascript functions.I cannot refer to functions like 'FileSystemObject'
Please help!
Please Note:
I need solution for above issue as I am working on mobile app which support javascript only
This function should work. Removes files synchronously
Pass removeself = true to remove the empty directory.
const path = require('path');
const fs = require('fs');
const rmDir = function (dirPath, removeSelf) {
if (removeSelf === undefined)
removeSelf = true;
try {
var files = fs.readdirSync(dirPath);
} catch (e) {
// throw e
return;
}
if (files.length > 0)
for (let i = 0; i < files.length; i++) {
const filePath = path.join(dirPath, files[i]);
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else
rmDir(filePath);
}
if (removeSelf)
fs.rmdirSync(dirPath);
};
Disclaimer: Not my code, copied from someone's gist(forgot the url).
I have a file , in javascript , that find all the directories that match the parameter.
And i got this error:
my code:
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
var home_path=getUserHome();
var findit = require('findit'),
path = require('path'),
finder = findit(path.resolve(home_path));
var myArgs = process.argv.slice(2)[0];
var filter1 = new RegExp(myArgs);
//This listens for directories found
finder.on('directory', function (dir) {
var directories = dir.split('\\');
var last= directories[directories.length-1].toLowerCase();
if(filter1.test(last)){
console.log('Directory: ' + dir );
}
});
(My code is a mess, i will clean it later)
How to fix that?
Why you didn't user the fs from Node, and look for dirs recursively? I think the error should be on the findit module...
That a look on https://nodejs.org/api/fs.html#fs_fs_readdir_path_callback or try instead the https://www.npmjs.com/package/recursive-readdir that also does it. I think that the things you use from the module findit, will be available there (like ways to ignore files)...
EDIT1: Example using recursive-readdir:
var recursive = require('recursive-readdir');
var filter1 = new RegExp(myArgs);
function ignoreFunc(file, stats) {
return !(stats.isDirectory() && filter1.test(path.basename(file)));
}
recursive('directory', [ignoreFunc] ,function (err, files) {
// Files is an array of filename (only the ones that matched the condition)
console.log(files);
});
#Moran, can you add a console.log directly in the callback of you "directory" event ?
finder.on('directory', function (dir) {
// Here
console.log(dir);
var directories = dir.split('\\');
var last= directories[directories.length-1].toLowerCase();
if(filter1.test(last)){
console.log('Directory: ' + dir );
}
});
To see what directory is problematic ? Then compare the rights applied on this folder and a directory that work, as "comverse" for example. It would help to find your error
I'm writing a program that should filter and print the files of the given directory from the command line based on the file type.
mymodule.js :
var fs = require('fs');
var result=[];
exports.name = function() {
fs.readdir(process.argv[2], function (err, list) {
for(var file in list){
if(list[file].indexOf('.'+process.argv[3]) !== -1){
result.push(list[file]);
}
}
});
};
And in my actual file
index.js:
var mymodule = require('./mymodule')
console.log(mymodule.name());
When the run the command
> node index.js SmashingNodeJS js //In SmashingNodeJS folder print all the .js files
The console.log is printing undefined, please let me know what I am doing wrong here and how to return/bind the content to exports.
I fixed it by following Bergi's comment above.
mymodule.js :
var fs = require('fs');
exports.name = function(print) { // Added print callback here as param
var result = [];
fs.readdir(process.argv[2], function (err, list) {
for (var file=0; file<list.length; file++) {
if (list[file].indexOf('.'+process.argv[3]) !== -1) {
result.push(list[file]);
}
}
print(result); // Calling back instead of return
});
};
And in index.js file:
var mymodule = require('./mymodule')
mymodule.name(console.log); // pass the console.log function as callback