Code Monkey home page Code Monkey logo

mvmv's Introduction

Build Status Build status Known Vulnerabilities Coverage Status License: MIT

mvmv

A NodeJS package that performs batch renaming and moving of files, with support for globbing wildcards * and ?.

You can use the package by importing it into your NodeJS scripts or as a command on the terminal.

Note:

Go on github for the latest version of this documentation.

Motivation

The typical implementations of the linux mv command do not support this rather intuitive usage:

> mv *.txt *.old

Installation

Installation is straightforward, as with most NPM packages.

Install mvmv globally to use it as a command line tool anywhere on the command console:

> npm install -g mvmv

Install it as a dependency to use it in your code:

> npm install --save mvmv

In-code Usage

mvmv.exec(src, dst, cb) accepts a callback and returns the number of successful files moved (returns NULL if file not found).

The callback is invoked after a move/rename attempt on an individual file. The arguments passed to the callback are: error, oldPath, newPath, index. index is the zero-based position of the file in the batch of files to be processed.

Example
const mvmv = require('mvmv').create();

mvmv.exec('*.txt', 'temp/*.old');

๐Ÿ‘ถ New in v0.9.9:

mvmv.execAsync(src, dst) is the async counterpart of mvmv.exec(). execAsync() returns a Promise. The Promise resolves to the number of files successfully moved. In case of errors, the Promise rejects to an Array of Error.

Example
const mvmv = require('mvmv').create();

mvmv.execAsync('*.txt', 'temp/*.old')
    .then(res => console.log(`Moved ${res} files.`))
    .catch(errors => errors.forEach(e => console.log(e)));

// In an async function
async function moveFiles(src, dst) {
    try {
        console.log(await mvmv.execAsync(src, dst));
    }
    catch (errors) {
        errors.forEach(e => console.log(e));
    }
}

Command-line Usage

With package installed globally:

> mvmv '*.txt' 'temp/*.old'

Execute the mvmv command without argument to display the usage information:

  Usage: mvmv [options] <source> <target>

  mvmv command moves (or renames) files specified by <source> to destination names specified by <target>.
  mvmv supports * and ? globbing wildcards for specifying file names pattern.
  If wildcards are used, <source> and <target> must be wrapped in quotes.
  Multiple consecutive * wildcards in <source> are treated as one single * wildcard.
  The file will not be moved if a file with the same name already exists at the target location.


  Options:

    -V, --version      output the version number
    -i, --interactive  Prompts for confirmation before each move operation.
    -s, --simulate     Dry-runs the move operations without affecting the file system.
    -v, --verbose      Prints additional operation details.
    -h, --help         output usage information
Tip!

You can invoke mvmv without prior installing it by using npx (bundled with NPM since v5.2.0):

> npx mvmv '*.txt' 'temp/*.old'

Caveats

  • mvmv does not overwrite existing files.
  • mvmv treats consecutive * wildcard in the source glob pattern as a single *. (This does not apply to the destination glob pattern.)
  • globbing patterns on the command line must be enclosed in quotes to prevent wildcard expansion by the shell. An alternative is to turn shell globbing off. See StackOverflow thread for more information.

How It Works

Wildcards in the destination glob pattern correspond to the wildcards of the same type appearing in the source glob pattern, matched by the order in which they appear.

Example
mvmv '**-*-lines-*?-*?.txt' '*_*-lines-s?e?.txt'
      |  |        |  |       | |        | |
      |  |        |  +-------|-|--------|-+
      |  |        +----------|-|--------+
      |  +-------------------|-+
      +----------------------+

(Reminder: consecutive * wildcards in the source glob pattern are treated as a single *.)

Demonstration

mvmv-demo2

mvmv's People

Contributors

dienluong avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

mvmv's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.