Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first file with a .txt extension in a directory with nodejs?

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?

like image 373
Rolando Avatar asked Jul 08 '13 01:07

Rolando


People also ask

How do I find a file in NodeJS?

To find the files that match a pattern using Node. js, install and use the glob module, passing it a pattern as the first parameter and a callback function as the second. The function gets called with a potential error object as the first parameter and the matching files as the second.

How do I read a directory in NodeJS?

The fs. readdir() method is used to asynchronously read the contents of a given directory. The callback of this method returns an array of all the file names in the directory. The options argument can be used to change the format in which the files are returned from the method.

What is the file extension of NodeJS file?

Module files can have either a . js, . node, or . json file extension.

How to get all files in the directory using Node JS?

We will be using Node.js fs core module to get all files in the directory, we can use following fs methods. fs.readdir (path, callbackFunction) – This method will read all files in the directory.You need to pass directory path as the first argument and in the second argument, you can any callback function.

How to find all files in a directory with a particular extension?

All the files in a directory with a particular extension can be found using the listdir () method of the os module in Python. The os.listdir () method is used to get the list of files and directories in the particular mentioned directory. The endswith () method is a member of a string class that checks if a string ends with a certain suffix.

How to search for files with txt extension in Python?

Using a for loop, you can search for files with .txt extension using glob (). * denotes all files with a given extension. In this example, we use endswith () method to check the .txt extension. Using a for loop, iterate through each file of directory /my_dir. Check if the file has extension .txt using endswith ().

How to list all files present in a given directory path?

For example, directory_path/*.txt to list all text files present in a given directory path. Here the * means file name can be anything, but it must have a txt extension. The gob.glob (pathname) method returns a list of files that matches the path and pattern specified in the pathname argument. in this case, it will return all text files.


2 Answers

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

for(var i in files) {
   if(path.extname(files[i]) === ".txt") {
       //do something
   }
}
like image 158
everconfusedGuy Avatar answered Sep 20 '22 13:09

everconfusedGuy


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.
})
like image 30
victommasi Avatar answered Sep 19 '22 13:09

victommasi