简体   繁体   中英

Gulp-rename files with ordinal numbers - starting index from the higest number in folder

I want to write a gulp task where all the files from one folder being moved to another, renamed using numbers.

I have this task so far:

  var index = 0;


gulp.task("jpg", function () {
    return gulp.src('img/new/**.{jpg,JPG}')
            .pipe(chmod(666))
            .pipe(rename(function (path) {
                path.basename = (index++);
                path.dirname += "/full_size";
                path.extname = ".jpg";
                return path;
            }))
            .pipe(gulp.dest('img/gallery'));
});

I was wondering how I can write script which would check what is the highest numbers already in the folder and update var index accordingly so files won't be overwritten.

With gulp I almost do not have experience. I guess it can be done much more efficiently. I tried it with another directory structure and it work for me. First you must require file system module so put this on the top of your gulp file:

const fs = require('fs');

Here is the modified gulp task:

/**
 * Gulp task edited by Georgi Naumov
 * gonaumov@gmail.com for contacts
 * and suggestions.
 */
gulp.task("jpg", function () {
    var files = fs.readdirSync('img/gallery/full_size/'), index = 0;

    // here we will find maximum number of index
    // keep in mind that this is very inefficient.
    files.forEach(function (currentFile) {
        var currentIndex = (/^([0-9]+)\.jpg$/i.exec(currentFile) || [, false])[1];
        if (currentIndex && parseInt(currentIndex) >= index) {
            index = ++currentIndex;
        }
    });

    return gulp.src('img/new/**.{jpg,JPG}')
        .pipe(chmod(666))
        .pipe(rename(function (path) {
            path.basename = (index++);
            path.dirname += "/full_size";
            path.extname = ".jpg";
            return path;
        }))
        .pipe(gulp.dest('img/gallery'));
});

If the performance is important in this case we can execute shell command, which can take the file with the greatest number, but the task will no longer be platform independent.

Edit:

I think that the isolating of logic to find the maximum number in the package is a good idea. So I just published npm package. You can install and use it.

For installation you must use:

npm install --save npm-max-dir-index

After this you can use it in this way:

const maxDirIndex = require('npm-max-dir-index');

/**
 * Gulp task edited by Georgi Naumov
 * gonaumov@gmail.com for contacts
 * and suggestions.
 */
gulp.task("jpg", function () {
    var index = maxDirIndex('img/gallery/full_size/', '^([0-9]+)\.jpg$');    

    return gulp.src('img/new/**.{jpg,JPG}')
        .pipe(chmod(666))
        .pipe(rename(function (path) {
            path.basename = (index++);
            path.dirname += "/full_size";
            path.extname = ".jpg";
            return path;
        }))
        .pipe(gulp.dest('img/gallery'));
});

Here can be read package documentation (I just updated the docs):

https://www.npmjs.com/package/npm-max-dir-index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM