Change the way my nodejs app activates - javascript

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..");
}

Related

Find first file with given filename

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...
}

Javascript,nodejs: give a "string not found" messege on console.log

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] [filename] [ext]
i would also like it to announce: Nothing found in a console.log every time that
a word wasn't found.
ive tried
if (!regex.test(fileContent)) {
console.log(`Noting found`);
it works only if you have one file without your word, but if not ,it loops.
for example if you have 4 files and one of them has the string it wil show
Your word was found in directory: [file]
Noting found
Noting found
Noting found.
So, how can i stop the loop after one !found console.log and how can i prevent it from showing in case of something has found?
const path = require('path');
const fs = require('fs');
function searchFilesInDirectory(dir, filter, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
const files = fs.readdirSync(dir);
const found = getFilesInDirectory(dir, ext);
found.forEach(file => {
const fileContent = fs.readFileSync(file);
const regex = new RegExp('\\b' + filter + '\\b');
if (regex.test(fileContent)) {
console.log(`Your word was found in directory: ${file}`);
}
});
}
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[3], process.argv[4]);
Change:
if (!regex.test(fileContent)) {
console.log(`Noting found`);
// ...
to:
if (!printed && !regex.test(fileContent)) {
console.log(`Noting found`);
printed = true;
// ...
and make sure that you have a variable called printed defined in outer scope, originally falsy.

what is the extname for the file .vimrc

A question stumbled me around the word extname! Attention please! I'm not ask for a solution, the major point is the word extname.
In the home page of the https://scriptoj.com/, I found a question which demands completing a function called extname, through which input is a filename and output is the extname.
const extname = (filename) => {
/* TODO */
}
when I submitted the code bellow,
const extname = (filename) => {
/* TODO */
var dotIndex = filename.lastIndexOf(".");
var extname = "";
if(dotIndex != -1){
extname = filename.substr(dotIndex);
}
return extname;
}
the error catcher warned me that
if the input is '.hello', the '' should come out but not the '.hello'
if the input is '.hello', the '' should come out but not the '.hello'
Then simply check if dotIndex is not 0 and do not substr the name if this is the case.

How to delete a folder in my local using javascript

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).

events.js:142 error in node.js

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

Categories

Resources