2017-07-13 14 views
0

ソースから同じ名前のファイルを削除することはできますか?たとえば、のは、私は先のフォルダに重複していないファイルだけをしたいソースの両方のフォルダを選択すると、私は次のフォルダ構造存在する場合は、重複を削除してください

a 
---file1.txt 
---file2.txt 
---file3.txt 
b 
---file1.txt 

を持っているとしましょう。その結果、上記の例では

result 
    ---file2.txt 
    ---file3.txt 

オプションのだろう、私はフィルタリングして、別のフォルダに書き込みを何とか複製することができれば、それは素晴らしいことです。 重複すると、明示的に名前で重複していることを意味します。ファイルの内容は重要ではありません。

答えて

1

それはそこに着くが、これを試してしばらくかかった:

var gulp = require('gulp'); 
var fs = require('fs'); 
var path = require('path'); 
var flatten = require('gulp-flatten'); 
var filter =  require('gulp-filter'); 

var folders = ['a', 'b', 'c']; // I just hard-coded your folders here 

    // this function is called by filter for each file in the above folders 
    // it should return false if the file is a duplicate, i.e., occurs 
    // in at least two folders 
function isUnique(file) { 

    console.dir(file.history[0]); // just for fun 
    var baseName = file.history[0].split(path.sep); 
    baseName = baseName[baseName.length - 1]; 

    // var fileParents = '././'; 
    var fileParents = '.' + path.sep + '.' + path.sep; 
    var count = 0; 

    folders.forEach(function (folder) { 
    if (fs.existsSync(fileParents + folder + path.sep + baseName)) count++; 
     // could quit forEach when count >= 2 if there were a lot of folders/files 
     // but there is no way to break out of a forEach 
    }); 

    if (count >= 2) { // the file is a duplicate   
    fs.unlinkSync(file.history[0]); // remove from 'Result' directory 
    return false; 
} 
else return true; 
} 

gulp.task('default', ['clump'], function() { 
    // create a filter to remove duplicates 
    const f = filter(function (file) { return isUnique(file); }, {restore: true, passthrough: false}); 

    const stream = gulp.src('./result/*.txt') 
    .pipe(f); // actually do the filtering here 

    f.restore.pipe(gulp.dest('duplicates')); // new stream with the removed duplicates 
    return stream; 
}); 

    // 'clump' runs first 
    // gathers all files into result directory 
gulp.task('clump', function() { 
    return gulp.src('./**/*.txt')     
    .pipe(flatten()) // because the original folder structure in not wanted 
    .pipe(gulp.dest('result')); 
}); 

は「一息」で、それを実行します。デフォルトのタスクは、最初に「clump」タスクを起動します。

あなたのOPでは、特定のバージョンの複製ファイルを保持する必要はありませんでした。最新のように、私はここでそれを心配していません。 'Result'フォルダに、file1.txt(1つのフォルダのバージョン)とfile1.txt(別のフォルダの)などの重複したファイルの各バージョンが必要な場合は、明らかに '塊状の仕事。

これがうまくいくかどうか教えてください。

関連する問題