11

The directory all my files are in is: '/usr/home/jordan' and I have many files under there (in the directory itself, but one file that is named with a .txt extension.

With nodejs and fs, I want to put that first file (or any file) with a txt extension into "mytxtfilepath". I only have a single .txt file in the whole directory (amongst many other files but with different extensions) The single .txt file could be named ANYTHING, I cannot guarantee what the name will be at any given time, only that it ends in .txt:

var homedir = "/usr/home/jordan";
var mytxtfilepath=homedir + "???????";
fs.readfile(mytxtfilepath, function(err,data) {
  console.log(data);
});

How do I put the correct path to my txt file without hardcoding the name of the txt file itself?

Rolando
  • 58,640
  • 98
  • 266
  • 407

5 Answers5

17
var files = fs.readdirSync(homedir);
var path = require('path');

for(var i in files) {
   if(path.extname(files[i]) === ".txt") {
       //do something
   }
}
codingninja
  • 1,360
  • 2
  • 13
  • 24
everconfusedGuy
  • 2,709
  • 27
  • 43
  • I have multiple files in the directory, but only one .txt file. – Rolando Jul 08 '13 at 02:05
  • @dandavis Since when `filter` is faster than loop? Since when regexps are faster than exact comparison? You're wrong. – polkovnikov.ph May 09 '17 at 13:53
  • @polkovnikov.ph: path.extname() was userland, which then ran slower than rx.test(). for has always been faster than filter, i wouldn't argue otherwise... – dandavis May 09 '17 at 17:57
1

You can use Glob Module as well. It works fine for me!

var glob = require( 'glob' );  
var myPath= "/fileFolder/**/*.txt";

glob(myPath, function (er, files) {
    // Files is an array of filenames.
    // Do something with files.
})
sphinks
  • 3,048
  • 8
  • 39
  • 55
victommasi
  • 339
  • 3
  • 10
0

You can use fs.readdir to list the files and find the one that ends with .txt:

var myPath = "/usr/home/jordan";
fs.readdir(path, function(fileNames) {
    for(var i = 0; i < fileNames.length; i++) {
      var fileName = fileNames[i];
      if(path.extname(fileName) === ".txt") {
        fs.readfile(path.join(myPath,fileName), function(err,data) {
          console.log(data);
        });
        break;
      }
    }
  }
);

Note this requires path so add var path = require("path") at the top.

go-oleg
  • 19,272
  • 3
  • 43
  • 44
0

You can use fs.readdir and path.extname

var fs = require('fs')
  , path = require('path');

function getFileWithExtensionName(dir, ext) {
  fs.readdir(dir, function(files){
    for (var i = 0; i < files.length; i++) {
      if (path.extname(files[i]) === '.' + ext)
        return files[i]
    }
  });
}

var homedir = "/usr/home/jordan";
var mytxtfilepath= getFileWithExtensionName(homedir, 'txt')
fs.readfile(mytxtfilepath, function(err,data) {
  console.log(data);
});
mquandalle
  • 2,600
  • 20
  • 24
0

The lazy solution:

npm install --save readdir

and then

const {read} = require('readdir');
read("/usr/home/jordan", "**/*.txt", (err, paths) =>
    console.log(paths)
);
polkovnikov.ph
  • 6,256
  • 6
  • 44
  • 79