Skip to content

Instantly share code, notes, and snippets.

@scriptex
Last active April 10, 2023 23:30
Show Gist options
  • Save scriptex/20536d8cda36221f91d69a6bd4a528b3 to your computer and use it in GitHub Desktop.
Save scriptex/20536d8cda36221f91d69a6bd4a528b3 to your computer and use it in GitHub Desktop.

Revisions

  1. scriptex revised this gist May 26, 2019. 1 changed file with 8 additions and 10 deletions.
    18 changes: 8 additions & 10 deletions rename.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,16 @@
    const fs = require('fs');
    const path = require('path');
    const args = process.argv.slice(2);
    const dir = args[0];
    const match = RegExp(args[1], 'g');
    const replace = args[2];
    const files = fs.readdirSync(dir);
    const { join } = require('path');
    const { readdirSync, renameSync } = require('fs');
    const [dir, search, replace] = process.argv.slice(2);
    const match = RegExp(search, 'g');
    const files = readdirSync(dir);

    files
    .filter(file => file.match(match))
    .forEach(file => {
    const filePath = path.join(dir, file);
    const newFilePath = path.join(dir, file.replace(match, replace));
    const filePath = join(dir, file);
    const newFilePath = join(dir, file.replace(match, replace));

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

    // Usage
  2. scriptex revised this gist Mar 13, 2019. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions rename.js
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,7 @@ const replace = args[2];
    const files = fs.readdirSync(dir);

    files
    .filter(file => {
    return file.match(match);
    })
    .filter(file => file.match(match))
    .forEach(file => {
    const filePath = path.join(dir, file);
    const newFilePath = path.join(dir, file.replace(match, replace));
  3. scriptex created this gist Nov 23, 2017.
    21 changes: 21 additions & 0 deletions rename.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    const fs = require('fs');
    const path = require('path');
    const args = process.argv.slice(2);
    const dir = args[0];
    const match = RegExp(args[1], 'g');
    const replace = args[2];
    const files = fs.readdirSync(dir);

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

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

    // Usage
    // node rename.js path/to/directory 'string-to-search' 'string-to-replace'