0

How do I search for a specific file by name and extension in a working directory, regardless of the file's path from inside that working directory?

For example, if we are searching for foo.txt inside our first working directory, but it's located within a file path like the example below

workDir/folder2/is/foo/directory/foo.txt

Then say for further example, we want to then search for foo.txt inside our second working directory, but the file is located within a different file path like below

workDir/folder11/this/way/to/foo.txt

How would I find foo.txt regardless of the file's path, inside a working directory?

I am Using the below JavaScript code in my NodeJS project's controller.js file to try and achieve this.

NOTE: In real-time, every working directory this would be used on, would be named differently

controller.js

var fooPath = GetFooPath(data, path.join(workDir, "folder11/"));
            if (fooPath == -1) {
                console.log("Cannot find the foo file!");
                return;
            }
4
  • You have to look for the file recursively in every subdirectory, using fs.readdirSync. Commented Jul 22, 2022 at 13:12
  • So in other words I would have to specify every first folder to enter in order to achieve this, folder2 folder3 etc, then search for the file inside of those directories? Sorry I am still new to JavaScript and NodeJS
    – Morsmalleo
    Commented Jul 22, 2022 at 13:20
  • You have to write a recursive function that searches for the file in a given directory. You call this function initially with the root directory, but it calls itself with the direct subdirectories of the given directory. Commented Jul 22, 2022 at 13:28
  • Yeah so I'm still new to JS, so you'd have to give me an example on that with the code snippet above if possible please.
    – Morsmalleo
    Commented Jul 24, 2022 at 7:48

1 Answer 1

1

You could use globbing

glob("absolute/path/to/workDir/**/*.txt", options, 
    function (error, files) {
   // files is an array of filenames.
   // If the `nonull` option is set, and nothing
   // was found, then files is ["**/*.js"]
   // error is an error object or null.
 })

NPM glob

9
  • I'll give this one a try and see if it works.
    – Morsmalleo
    Commented Jul 22, 2022 at 23:58
  • No this doesn't seem to want to work for me, tried as much as I could and still nothing, any other suggestions???
    – Morsmalleo
    Commented Jul 24, 2022 at 7:45
  • This should work. What paths have you looked for ? What is your glob string ? Commented Jul 24, 2022 at 10:51
  • I specify glob(workDir which gives an absolute path to the working directory I do believe, using node-homedir & path. The problem after this is I still need to specify folder11 or folder3 in order to find the correct file I need
    – Morsmalleo
    Commented Jul 24, 2022 at 16:16
  • I could be doing it wrong, I have stated I am still fairly new to JS & node, if possible could you show me using my code snippet above sir???
    – Morsmalleo
    Commented Jul 24, 2022 at 16:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.