You can easily read directories and sub-directories to get all files in Node.js but sometimes you might need to find files by their names, extensions, or a pattern in NodeJS. It might sound difficult but fortunately, it's not. Show
You can easily search into a directory (folder) with Node.js for a specific file or files. For that, you can use the filename, file extension, or a regular expression. In this article, I will walk you through all the methods to find files in a directory and its sub-directories with pure JavaScript and an external package. You can search for files in Node.js using the following methods:
Let's see all the methods in detail step by step. Find Files By Extension in Node.jsTo get files by using extensions, I have created a function called First, we have to get all the files from the provided directory using In the next step, you have to loop over the files you got from the In the above code, you can see I am doing the same. Getting all the files and looping over them using
In the above example, I have used the first method. For any file that matches our target extension, I am pushing that file name in the Method 1: Now, I am calling the Method 2: In this method, I am checking if the file name ends with the target extension type using the
Search Files By Name in Node.jsTo search for files by name, you have to follow the same process to get all the files from a directory. Then you can loop over those files to check if the file name matches the name you are looking for. The only difference, in this case, is how we compare the file names. You can compare those names in 3 ways:
Let's learn about all these methods with examples. Method 1: You can get the filename of each file inside the One thing you should keep in mind while using this technique is that you have to provide the exact filename. It can not check for partially matching names. But if you don't want to use exact names while searching, you can use the other two methods.
Method 2: In this method, I am checking if the file starts with the provided name with the You don't need the exact name of a file. You have to provide part from the start. If you want to find files with partially matching names from any part of the file, you can use our next method. Method 3: Instead of checking if the file starts with a piece of string, you can check if the file includes that string with the You
can see when I am calling the Get Files By Matching Pattern Using Glob in Node.jsSo far we have seen how to find files using javascript. We wrote functions according to our requirements. If you have a complex directory structure with many directories and sub-directories, it will be a good idea to use an external package. Now I will show you how you can use the glob package to search for files. With this package, you can find files from multiple directories and sub-directories using some pattern like a regular expression. First, initialize a Node.js project by creating the package.json file in the root of your project folder. You have to create this file using the following command: In
the next step, you have to install the Now you can use this package in your project. I have created a file called The In the callback function, you get 2 things i.e. error and matched files. You can check if there is any error and you will get the files that match the pattern. In this example, I am calling the If you want to get all files from a directory including its sub-directories, you have to use a different pattern. There are different types of patterns that you can use with the glob package.
List of Common Glob PatternsYou can create a complex pattern for your search if you need to use different characters. You can learn more about those by following the official documentation of this package. I will show you some frequently used patterns that you can use in your project. These patterns are:
ConclusionKnowing how to work with files is an important skill for every developer. You can get all files from a directory or you can search any specific types of files in Nodejs. This is what you have learned in this article. I have shown you different methods to find files from a directory and sub-directories. You can use filenames, extensions, or any pattern to identify file types. Use How to get files from folder in JavaScript?“javascript how to list all files from a folder” Code Answer's. const testFolder = './tests/';. const fs = require('fs');. fs. readdir(testFolder, (err, files) => {. files. forEach(file => {. console. log(file);. How do you get a list of the names of all files present in a directory in node JS?You can use the f. readdir() method to list all files available in a directory in Node. js. This method asynchronously reads the contents of the given directory and returns an array of the file names excluding .
What is filename in url?The filename is the last part of the URL from the last trailing slash. For example, if the URL is http://www.example.com/dir/file.html then file. html is the file name.
How do I scan a directory in Javascript?How to scan a folder for documents with javascript. var dir = "/videos";. var fileextension = ".mp4";. $. ajax({. //This will retrieve the contents of the folder if the folder is configured as 'browsable'. url: dir,. success: function (data) {. // List all mp4 file names in the page.. $(data).. |