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?
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.
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.
Module files can have either a . js, . node, or . json file extension.
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.
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.
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 ().
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.
var files = fs.readdirSync(homedir);
var path = require('path');
for(var i in files) {
if(path.extname(files[i]) === ".txt") {
//do something
}
}
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.
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With