gulpgulp-rename

Renaming a file with gulp rename and adding an index


I need to rename a batch of photos adding an index to them, like 'image-1-tmb' or 'image-23-tmb'. I have already searched this and didn't find it, didn't even come close to finding it.

This is my actual code:

gulp.task('rsz_tmb_menu',function(){
 return gulp.src('./zips/**/*.{jpg,JPG}', { base: './zips' })
 .pipe(imageResize({ 
   width : width_tmb_menu,
   height : height_tmb_menu,
   crop : true,
   quality : 0.6,
   imageMagick : true,
   upscale : false
 }))
 .pipe(gulp.dest('./images/tmb_menu'));
});

Solution

  • Use gulp-rename:

    var rename = require("gulp-rename");
    

    then add to your pipe: gulp.task('rsz_tmb_menu',function(){

      var index = 0;
    
    gulp.src('your_glob')
      .pipe(your processing func)
      .pipe(rename(function (path) {
      path.basename += ("-" + index++);
     }))
    .pipe(...dst...)