Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Files Using Gulp w/ Specific Pattern

I have a Gulp task that needs to copy the contents from one directory to another and rename each file with a specific pattern. Basically, if I have main.css, I need it to create a copy of the main.css and name it main_main_bk.css (don't ask - it's a convention that I need to abide by). To be clear, I need to add a _ followed by the name of the file currently being processed followed by _bk. How can I read what the name of the current file is in Gulp?

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

gulp.task('copit', function() {
  gulp.src('asset/css/*.css')
    .pipe(rename({suffix: '_' + ??? + '_bk'}))
    .pipe(gulp.dest('asset/test'));
});

So,

asset/css/main.css -> asset/test/main_main_bk.css
asset/css/style.css -> asset/test/style_style_bk.css

Etc.

like image 984
chrisjnas Avatar asked Feb 22 '26 03:02

chrisjnas


1 Answers

You can use basename and rename it via function

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

gulp.task('copit', function() {
  gulp.src('asset/css/*.css')
    .pipe(rename(function (path) {
        path.basename += "_" + path.basename + "_bk";
    }))
    .pipe(gulp.dest('asset/test'));
});
like image 178
onetwo Avatar answered Feb 23 '26 15:02

onetwo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!