Find first file with given filename - javascript

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

Related

Is this possible with Gulp? Gulp Change Text and Dest

I don't know what this is possible.
files
a.html / b.hml
c.css / d.css
There are two changes in Html and Css
Each HTML has a changed Css(c.css,d.css).
The path of Css is as follows
<link src="/style/sample/c.css">
If a Css file has been changed, verify that the CSS file has been declared in the changed HTML.
Change the path of the css file.
Create a new file using Gulf's dest.
That's my code here.
async function ds(cd) {
const branch = branchName.replace("/", "-");
let fileHtml = [],
fileCSS = [],
fileImage = [];
// npm package git-status,
// Look for files that are up on git stage.
await gitStatus((err, data) => {
data.map((e) => {
item = e.to.replace("WebContent/resources", "./resources"); // change path
// Check only Resources
if (item.indexOf("./resources/") !== -1) {
if (item.indexOf("html") !== -1) {
fileHtml.push(item); // get Html List T^T
return fileHtml;
}
if (item.indexOf("css") !== -1) {
// css
fileCSS.push(item);
return fileCSS;
}
if (item.indexOf("img") !== -1) {
// img
fileImage.push(item);
return fileImage;
}
}
});
cd();
});
//I think it's possible with JavaScript, but I don't know what to do with Gulp.
fileHtml.forEach((e) => {
let files = fs.readFileSync(__dirname + e, "utf8"); //해당 HTML 파일
let splitFile = files.split(/\r?\n/);
// 각각의 HTML 파일 내부에서
let result = splitFile.forEach((j, i) => {
// 위에서 찾은 css 파일이 있는지 찾아서
fileCSS.forEach((v) => {
if (j.indexOf(v) >= 0) {
let result = j.replace("/resources/", "/" + branch + "/");
console.log(result);
// console.log("페이지는", e);
// console.log("파일은", v);
}
});
});
});
}

How can I overwrite and append data to the same file multiple times in node js

I have a "clients.txt" file where I have a list of emails. I try to run a program for sending emails where I chose a number of emails to use from the file, in that case the number is 2. After I use the two emails I want to overwrite "clients.txt" without them. The problem is when I try to run the code just for one single time every thing is working! but if I make a loop something is wrong. Looking forward to see any help from you guys. Thanks! I add the code bellow. PS: Sorry for my bad english!
function readEmails(){
const fs = require('fs');
clients_list = fs.readFileSync('clients.txt', 'utf8').split('\n');
let filtered = clients_list.filter(function (el) {
return el != null && el != '';
});
return filtered
}
function dump_array(arr, file){
let fs = require('fs');
let file = fs.createWriteStream(file);
file.on('error', function(err) { /* error handling */ });
arr.forEach(function(v) { file.write(v + '\n'); });
file.end();
}
while_var = 0;
while (while_var < 2){
while_var ++;
let all_clients = readEmails();
let selected_clients = [];
if (all_clients.length > 0){
selected_clients = all_clients.splice(0,2);
dump_array(all_clients, 'clients.txt');
console.log(selected_clients);
}else{
console.log('No more clients')
}
}
const fs = require('fs');
function readEmails(){
const clients_list = fs.readFileSync('clients.txt', 'utf8').split('\n');
const filtered = clients_list
// clear false, 0 and undefined too
.filter(el => !!el)
// remove extra spaces and \r symbols
.map(el => el.trim());
return filtered;
}
function dump_array(arr, file){
// Here you need sync method.
fs.writeFileSync(file, arr.join('\n'));
// And here was 'already declared' error in orginal code
}
let while_var = 0;
while (while_var++ < 2){
let all_clients = readEmails();
let selected_clients = [];
if (all_clients.length > 0){
selected_clients = all_clients.splice(0,2);
dump_array(all_clients, 'clients.txt');
console.log(selected_clients);
}else{
console.log('No more clients')
}
}

Javascript to download and process GTFS zip file

I try to download, unzip and process a GTFS file in zip format. Downloading and unzipping are working, but I get error message when try to use txt files with gtfs-utils module in gtfsFunc(). Output is undefined. Delays are hardcoded just for testing purpose.
const dl = new DownloaderHelper('http://www.bkk.hu/gtfs/budapest_gtfs.zip', __dirname);
dl.on('end', () => console.log('Download Completed'))
dl.start();
myVar = setTimeout(zipFunc, 30000);
function zipFunc() {
console.log('Unzipping started...');
var zip = new AdmZip("./budapest_gtfs.zip");
var zipEntries = zip.getEntries();
zip.extractAllTo("./gtfsdata/", true);
}
myVar = setTimeout(gtfsFunc, 40000);
function gtfsFunc() {
console.log('Processing started...');
const readFile = name => readCsv('./gtfsdata/' + name + '.txt')
const filter = t => t.route_id === 'M4'
readStops(readFile, filter)
.then((stops) => {
const someStopId = Object.keys(stops)[0]
const someStop = stops[someStopId]
console.log(someStop)
})
}
As #ChickenSoups said, you are trying to filter stops files with route_id field and this txt doesnt have this field.
The fields that stops has are:
stop_id, stop_name, stop_lat, stop_lon, stop_code, location_type, parent_station, wheelchair_boarding, stop_direction
Perhaps what you need is read the Trips.txt file instead Stops.txt, as this file has route_id field.
And you can accomplish this using readTrips function:
const readTrips = require("gtfs-utils/read-trips");
And your gtfsFunc would be:
function gtfsFunc() {
console.log("Processing started...");
const readFile = (name) => {
return readCsv("./gtfsdata/" + name + ".txt").on("error", console.error);
};
//I used 5200 because your Trips.txt contains routes id with this value
const filterTrips = (t) => t.route_id === "5200";
readTrips(readFile, filterTrips).then((stops) => {
console.log("filtered stops", stops);
const someStopId = Object.keys(stops)[0];
const someStop = stops[someStopId];
console.log("someStop", someStop);
});
}
Or if what you really want is to read Stops.txt, you just need to change your filter
const filter = t => t.route_id === 'M4'
to use some valid field, for example:
const filter = t => t.stop_name=== 'M4'
Stop data don't have route_id field.
You should try other data, such as Trip or Route
You can look at the first row in your data file to see which field do they have.
GTFS data structure here

Change the way my nodejs app activates

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

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.

Categories

Resources