0

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));
  • 2
    You question is unclear, but here is a tip that might help you. Use "__dirname", it is a env variables that refers to the absolute path of the current file. – Nizar Zizoune Sep 03 '21 at 13:52
  • Also consider using the `path` module. – evolutionxbox Sep 03 '21 at 13:54
  • 1
    @NizarZizoune `__dirname` is only the current folder, not the current file – Bao Huynh Lam Sep 03 '21 at 13:59
  • @BaoHuynhLam Good catch thank you. Yes. it refers to the directory containing the current file. – Nizar Zizoune Sep 03 '21 at 14:02
  • 1
    Does this answer your question? [Proper way to reference files relative to application root in Node.JS](https://stackoverflow.com/questions/13051961/proper-way-to-reference-files-relative-to-application-root-in-node-js) – lejlun Sep 03 '21 at 14:13

2 Answers2

1

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.

Bao Huynh Lam
  • 974
  • 4
  • 12
0

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.

mpmcintyre
  • 614
  • 5
  • 16