0

I have images with different titles:

  1. !test.png
  2. 1101.png
  3. 12345.jpg
  4. image3.jpg
  5. image4.jpg
  6. image5.jpg
  7. image6.jpg
  8. image7.jpg
  9. test_test.png
  10. test.jpg
  11. test2.png

I need images with next titles:

  1. 101.png
  2. 102.png
  3. 103.jpg
  4. 104.png
  5. 105.png
  6. 106.jpg
  7. 107.jpg
  8. 108.jpg
  9. 109.jpg
  10. 110.jpg
  11. 111.png

Example: 101.png = "1" + "01.png" or "01.jpg", where:

  • 1” is variable I enter in the console when I type the gulp command (gulp 1)
  • 01..11.png” (or “01..11.jpg”) are just the names of the images in ascending order

I have this code, gulpfile.js:

import gulp from 'gulp';
import rename from 'gulp-rename';
import del from 'del';

const deleteImages = () => {
  return del(['build',], {force:true});
}

const renameImages = () => {
    return gulp.src('src/images/*.{jpg,jpeg,png,gif,wepb}')
    .pipe(rename({
      // code
    }))
    .pipe(gulp.dest('build'))
}

gulp.task('default', gulp.series(deleteImages, renameImages));

package.json:

{
  "name": "gulp-rename-images",
  "description": "Task for gulp",
  "version": "0.0.1",
  "license": "MIT",
  "main": "gulpfile.js",
  "type": "module",
  "devDependencies": {
    "gulp": "^4.0.2",
    "gulp-rename": "^2.0.0",
    "del": "^6.1.0"
  }
}

My structure:

My structure

Mac
  • 13
  • 3

1 Answers1

0

I got this code, gulpfile.js:

import gulp from 'gulp';
import rename from 'gulp-rename';
import del from 'del';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers'

const argv = yargs(hideBin(process.argv)).argv

const deleteImages = () => {
  return del(['build',], {force:true});
}

const renameImages = () => {
  var arg = argv.arg
  var index = 1
  return gulp.src('src/images/*.{jpg,jpeg,png,gif,wepb}')
  .pipe(rename(function (path) {
    if (index <= 9) {
      path.basename = arg + '0' + (index++)
    } else {
      path.basename = arg + '' + (index++)
    }
    return path;
 }))
  .pipe(gulp.dest('build'))
}

gulp.task('default', gulp.series(deleteImages, renameImages));

packgage.json:

{
  "name": "gulp-rename-images",
  "description": "Task for gulp",
  "version": "0.0.1",
  "license": "MIT",
  "main": "gulpfile.js",
  "type": "module",
  "devDependencies": {
    "del": "^6.1.0",
    "gulp": "^4.0.2",
    "gulp-rename": "^2.0.0",
    "yargs": "^17.5.1"
  }
}

In the console I write:

gulp --arg 1

Is there a prettier solution?

Mac
  • 13
  • 3