Node.js not only opening my code in a specific place [duplicate] - javascript

This question already has answers here:
Proper way to reference files relative to application root in Node.JS
(5 answers)
Closed 1 year ago.
My question is, is it possible to do so that when I have a code and a text file, but when I want to open or locate the text file, I dont need to specify it everytime where it is or like when I send it to someone else, they dont need to change the let file = C:\Users\etc...
My code:
const { readFile, readFileSync } = require('fs');
let file = 'C:\\Users\\eeroj\\Desktop\\word-counter\\TextFile2.txt';
function countRepeatedWords(sentence) {
const words = sentence.split(" ").filter(word => !!word);
const wordMap = {};
for (let word of words) {
const key = word.trim().toLowerCase();
const currentWordCount = wordMap[key];
wordMap[key] = (currentWordCount ?? 0) + 1;
}
const sortedEntries = Object.entries(wordMap).sort(([a,], [b,]) => a.localeCompare(b));
const sortedWordMap = Object.fromEntries(sortedEntries);
//returning the wordMap and the sorting of the wordMap
return sortedWordMap
return wordMap;
}
words = readFileSync(file).toString();
console.log(countRepeatedWords(words));

Yes you can, through the path (built-in Nodejs) module and the __dirname constant. The __dirname environment variable will automatically get the current folder's path for you, and then you can use path.join to join the current directory with the filename you want to access. Something like this
const path = require("path");
// Assuming that the current script file are inside the 'C:\\Users\\eeroj\\Desktop\\word-counter' folder
const file = path.join(__dirname, "TextFile2.txt");
// file = 'C:\\Users\\eeroj\\Desktop\\word-counter\\TextFile2.txt'
If you put "TextFile2.txt" in a relative position not in the current folder, you can also use path.join with the folder traversal syntax such as .. to go back a folder
const path = require("path");
// Assuming that the current script file are inside the 'C:\\Users\\eeroj\\Desktop\\word-counter' folder
const file = path.join(__dirname,"../../", "TextFile2.txt");
// file = "C:\\Users\\eeroj\\TextFile2.txt"
As a result, this way should work regardless of anywhere you put the project, even on a different machine, as long as the project folder structure stays relatively the same and the "TextFile2.txt" file is in the right place relative to the script file.

If your txt file is in the same directory as the script you can simply use the relative path with a '.' as
let file = "./TextFile2.txt"
Just note that the textfile will have to always have the same name.

Related

How do I browse directories for json files (JavaScript)?

I want to browse directories for json files and merge all json files into one json file per directory.
My javascript skill is at beginner level.
The javascript code needs to run by calling it in the Terminal, not in the browser.
you can make use of glob npm package to retrieve all json file paths in the directories.
glob : https://www.npmjs.com/package/glob
you can simply go with :
const glob = require("glob");
const fs = require('fs-extra');
async function processJsonData(){
let parentDirectoryPath = '<specify path here>';
let data = {};
let jsonFilePaths = glob.sync(`${parentDirectoryPath}/*/*.json`);
//this will return paths for all the json files
for (const jsonFilePath of jsonFilePaths) {
let content = await fs.readJsonSync(jsonFilePath);
Object.assign(data,content);
}
await fs.writeJson(<filePath>, data, {spaces: 2});
//here specify your file path
}
ps. I am a new contributor, might have misread the question.

Meyda feature extracted dat is different in command line and through nodejs

I tried to extract feature of an audio file using Meyda. But feature exytracted values are different when extracted using command line meyda and through javascript file
Command line
meyda 1600153162.63571.wav mfcc
Result
0.057628476337413304,0.025103534166687494,-0.0033402018588145435,-0.0050914619032203364,-0.005969043107840086,-0.009452339628697326,-0.0077781823184730455,-0.0024033016732702066,-0.0033368248853514206,-0.0012372591871309979,0.00003587458387239886,0.00293735474906295,0.0033156231532707036
1.4306079393227265,1.3713938566968262,1.2306487569743767,1.0515807610571597,0.8414768385049735,0.6060415036630535,0.37326507523088365,0.1654296077929904,-0.015392187854876595,-0.1652509593657213,-0.27571446879975764,-0.340564139593766,-0.36505384389267703
9.282664122642018,6.880236029290547,3.3870794551074606,1.7170683624244123,-0.29657914336587826,-3.18544774959499,-4.433789386396694,-3.7101490665746217,-2.816738558429225,-2.2736417577176273,-1.6636438686466657,-0.8537746809738949,-0.5116463089021152
25.65632094501052,22.93621824737729,16.08611763344924,7.490656903754756,-0.4358888007314062,-5.641656791337735,-7.229285832273481,-5.635856022395436,-2.1681330598896817,1.5575050036371694,4.296381195632149,5.45750656573637,5.059966218569768
23.469876801827922,20.984500104582974,15.80549272827184,10.064116650359527,3.957435073588796,-1.75506262168123,-5.904087102697238,-8.29320895086182,-9.037630914839253,-8.230609293960022,-6.604159793447832,-4.8505887091085365,-3.2390627275186175
29.14206006610766,25.50517483703385,19.279159148659456,13.806566615476791,7.715092034538,2.2364700767576764,-0.8129542016331001,-2.16772108652068,-2.6257181917727093,-2.1347711722007845,-1.0689231846996041,-0.6059541306434987,-0.9955650805848651
12.251538716256618,9.479093353756676,5.26726325506769,2.7893618666871762,0.10288164291841367,-3.121859159330943,-4.446188894370852,-3.5959072906303837,-2.5699522942015007,-1.8300687753240275,-0.7140618177046422,0.3644613187160053,0.8353185065087604
18.42772721964866,15.990459244575096,11.993313228734504,9.088259023158539,6.172351883886699,2.6632784721745213,-0.2683186290158647,-1.948275236251193,-2.793271841187546,-3.305172382572076,-3.6707865100252994,-3.638999985131219,-2.863263822827715
15.915610973257571,13.737073178518493,9.801934620786648,5.779403187264773,1.358952752885622,-2.5086411759816647,-5.187471901991046,-7.0034704192058985,-7.837140523422115,-7.437282553994893,-6.058991955183858,-4.360246975428363,-2.680629805396066
15.75090683856979,13.497089898444463,9.489154494388005,5.578283842168272,1.4567586737610156,-2.141429668615224,-4.380728646793593,-5.364310654901947,-5.049848052960211,-4.214979808517224,-3.9745754014376224,-4.076571890925421,-4.020714303609834
23.361729244701564,20.83103985201436,16.592090051349242,12.885538444506535,8.373777248668826,2.9768227085972145,-1.399644048138661,-4.090450595665753,-5.574775136428788,-5.787457864519816,-4.693018758118676,-3.100993535744204,-1.5625546377399475
12.793707716744393,10.197116076581045,5.819041457288568,2.4679725003955064,-0.33041998215142393,-2.607817847380143,-4.086120143731154,-4.779218992884254,-4.374465394096245,-3.118504640745296,-1.9666020102081847,-1.5140789377241373,-1.5130647010950313
3.2732387410942465,2.2453286293698977,0.9749483649128511,0.472715919767034,0.11249067642678866,-0.19927487640318992,-0.24286230087509772,-0.3645480814147444,-0.5060378572115204,-0.4793996411313405,-0.45934442588699886,-0.4680761055711004,-0.2967506177492105
Through javscript file
var express = require('express');
var app = express();
const fs = require('fs');
var Meyda = require('meyda');
var load = require('audio-loader')
let filename = "1600153162.63571.wav"
load(filename).then(function (buffer) {
const channelData = buffer.getChannelData(0)
const PaddingLength = (Math.pow(2,Math.round(Math.log2(channelData.length)+1)) - channelData.length)
let halfPaddingLength = parseInt(PaddingLength/2)
const pad1 = new Array(halfPaddingLength).fill(0);
const pad2 = new Array(PaddingLength - halfPaddingLength).fill(0);
let finalBbuffer = [...pad1,...channelData,...pad2]
console.log(finalBbuffer.length)
let mfccData = Meyda.extract('mfcc', finalBbuffer)
console.log("mfccData : ",mfccData);
});
Result :
[
249.11783051490784,
-90.61751411189829,
-12.253094024524968,
19.88245460444982,
-11.661965456271869,
-14.795375019626466,
7.19298966815922,
8.362884489124907,
3.9941283332736557,
-3.2158388656478287,
-2.0395393071161063,
-0.48849176751482837,
-3.6199623273626695
]
Why this difference . How to make them same ?
They're different because you're padding the buffer in your code, whereas the command line tool is splitting your file into multiple buffers with the default buffer size and returning MFCCs for each segment.
To make them the same, you could replicate the buffer chunking that the CLI performs in your code, rather than zero-padding the whole buffer, or you could make a CLI that zero pads the input. If you do the latter, we would certainly be interested in adding that functionality to the bundled CLI, so feel free to open an issue on our issue tracker to discuss if you go that route and wish to contribute.
Thanks for using Meyda!

Get javascript filepath in Windows Script Host

When using .js files that are run with Windows Script Host, is it possible to get the path of the .js file that is running (which is not the same as the current working directory)? In other words, I'm wanting something similar to PowerShell's $PSScriptRoot variable. Is there any way to get this?
The reason I want this is because the working directory when these scripts are executed is not always the same as the location of the .js file itself, but I want my code to reference things relative to the location of the .js file.
You can get this with Node Js
const path = require('path'); // you can install this with npm
// __dirname = C:\dev\project
// __filename = C:\dev\project\index.js
const dir= path.basename(__dirname);
const archive= path.basename(__filename);
const fullpath = path.join(dir, archive);
console.log('Dir:', dir); // Dir: test
console.log('Archive:', archive); // Archive: index.js
console.log('Dir + Archive:', fullpath); // Dir + Archive: project\index.js

get the middle folder of a file from a path in node.js

I have a file under "modules/test/main/main.js" and i want to get the "test" folder :
I tried this code but all i get is the main folder
path.basename(path.dirname('modules/test/main/main.js'))
You can split on / and than drop off last item as it is file name.
and than take the middle element
let path = `modules/test/main/main.js`
let middle = path.split(/\//g)
middle.pop()
console.log(middle[Math.floor(middle.length-1)/2])
You can use path.join() to go up two directories, and then get the basename of the directory:
const p = 'modules/test/main/main.js';
const dir = path.basename(path.join(p, '../..')); // 'test'

list all the files with the same extension as specified by the user

prints a list of files in a given directory, filtered by the extension of the files. The first argument will be the path to the directory we want to filter on, and the second argument, the type of the file that we need to print,
we must use readdir function .
like running this:
node fileJS.js /Users/admin/Desktop .docx
I've tried this code, but it will no return anything, for some reason.
can you please help me
var fs = require('fs'); //require node filesystem module
var path = require('path'); //require node path module (a couple of tools for reading path names)
var pathSupplied = process.argv[2];
var extFilter = process.argv[3];
function extension(element) {
var extName = path.extname(element);
return extName === '.' + extFilter;
};
fs.readdir(pathSupplied, function(err, list) {
list.filter(extension).forEach(function(value) {
console.log(value);
});
});
The problem is when you provide the extension argument starting with a . because in your filter function (extension) you are adding another dot when comparing (line: return extName === '.' + extFilter;).
Just invoke your current script like this:
node fileJS.js /Users/admin/Desktop docx
Or update your filter function to assume extFilter is provided with a starting dot.
Invoke the command like this:
node fileJS.js /Users/admin/Desktop docx
...without the dot in front of the extension you're looking for. Your code works then, I just tested it.

Categories

Resources