npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-counter

v0.1.2

Published

Recursively count the number of files within a directory, and all of its subdirectories. Apply filters to exclude files or directories from the total.

Downloads

3

Readme

file-counter

Recursively count the number of files within a directory, and all of its subdirectories. Apply filters to exclude files or directories from the total.

Install

file-counter can be used as either a command line utility or included in your projects or scripts where you need to know the number of files within a directory.

CLI

Install globally so you can run the file-counter command via the command line within a directory to get the total number of files within that directory and all sub-directories.

Via npm:

npm install -g file-counter

countFiles Method

If you need to get the total number of files for a task progress bar, or you’re evaluating the number of files within a directory for maintenance purposes within another script, you can use the countFiles method.

Via npm:

npm install file-counter

Via yarn:

yarn add file-counter

How to use

You can use file-counter two different ways, as a command line utility or as a utility for use within your package or project. Below are a few examples on how you can use it in those scenarios.

CLI

$ file-counter [options] [dir]

Options

The following options are available both for the CLI use of file-counter, as well as for the countFiles method. Please note that when using the --exclude-*, or excludeDirs, options that the defaults will no longer be applied.

Exclude Files - -f, --exclude-files [...files]

Depending on what you’re doing, you may find the need to exclude certain files from the total that is returned. You can do that by including a comma separated list of file names or regular expressions to exclude from the count tally.

By default, the following files are excluded from totals:

  • .DS_Store
  • .DS_Store?
  • .Spotlight-V100
  • .Trashes
  • ehthumbs.db
  • Thumbs.db

Here’s an example of using the -f, or --exclude-files, option:

Exlude any .DS_Store file and all Javascript (*.js) files:

$ file-counter -f '.DS_Store,(.*).js'
Exclude Directories - -d, --exclude-dirs [...directories]

The same goes for directories. You can use either the exact directory name or a regular expression to exclude directories, and their files, from being included in the total count.

By default, the following directories are excluded from totals:

  • .git
  • node_modules
  • vendor

Here are a few examples of using the -d, or --exclude-dirs, option:

Exclude .git and node_modules directories:

$ file-counter -d '.git,node_modules'

Exclude all directories (ie. only tally the files in the root directory):

$ file-counter -d '(.*)'
Verbose - -v, --verbose

Including the --verbose flag will output file names included in the total count.

$ file-counter -v

countFiles Method

In addition to the command line utility, you can also use the method that the utility relies on in your own projects and scripts where you would want to know the number of files in a directory.

countFiles({...options}, count = 0)

*Note: count is there because the method is recursively called while traversing the directories. Please do not prepopulate that argument unless you want to increment/decrement the count by that value.

Options

countFiles({
    dir: '...',
    excludeFiles: ['...',],
    excludeDirs: ['...',],
    includeLogs: [true|false],
    verbose: [true|false],
})

Here’s an example where we use the countFiles method in combination with the progress package to show a progress bar during the processing of a Node script.

import path from 'path';
import { countFiles } from 'file-counter';
import ProgressBar from 'progress';

const ROOT_DIR = path.join(__dirname, '../');
const totalFiles = countFiles({
    dir: ROOT_DIR,
});

const bar = new ProgressBar(':bar', { total: totalFiles });

...
(iterate over files, doing stuff)
bar.tick();
...

And, if you’re curious about the progress package referenced above, you can find out more about that, here.

License

MIT