Skip to content

Instantly share code, notes, and snippets.

@hpneo
Created January 2, 2016 16:36
Show Gist options
  • Save hpneo/c4da1ca88e56e6164e36 to your computer and use it in GitHub Desktop.
Save hpneo/c4da1ca88e56e6164e36 to your computer and use it in GitHub Desktop.

Revisions

  1. hpneo created this gist Jan 2, 2016.
    21 changes: 21 additions & 0 deletions rename.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    var fs = require('fs'),
    path = require('path'),
    args = process.argv.slice(2),
    dir = args[0],
    match = RegExp(args[1], 'g'),
    replace = args[2],
    files;

    files = fs.readdirSync(dir);

    files.filter(function(file) {
    return file.match(match);
    }).forEach(function(file) {
    var filePath = path.join(dir, file),
    newFilePath = path.join(dir, file.replace(match, replace));

    fs.renameSync(filePath, newFilePath);
    });

    // Usage
    // node rename.js path/to/directory '\s' '-'