Search code examples
filegulpcopy

Gulp – How to copy files into all subfolders of a folder


I'd like to copy one file into each subdirectory of a directory with gulp. This code doesn't seem to do the trick, although * should stand for any number of characters if I am not mistaken.

// Copy the main.css
    gulp.src('./styles/main.css')
       .pipe(gulp.dest('./test-courses/*/'));

Is gulp not able to detect all subfolders itself? Is it even possible to write anything like this with Gulp?

Thanks for any help


Solution

  • Try this:

    const glob = require('glob');
    
    // get an array of subdirectories under test-courses, 
    const subDirectories = glob.sync('./test-courses/*/');
    
    // console.dir(subDirectories);
    
    // Copy the main.css
    gulp.task('default', (done) => {
    
      // run the pipeline for each subDirectory
    
      subDirectories.forEach(function (subDirectory) {
    
        return gulp.src('./styles/main.css')
          .pipe(gulp.dest(subDirectory));
      });
    
      done();
    });
    

    glob.sync returns an array.

    I believe that gulp.dest takes a simple string (not a glob) or a function which returns a string so your ('./test-courses/*/') will not work.