How can I use a glob to ignore files that start with an underscore?
Asked Answered
T

5

23

I am using this file matching in gulp:

'content/css/*.css'

But I would like to only include the files that do not start with an underscore. Can someone tell me how I can do this?

Here's the code:

gulp.task('less', function () {
    gulp.src('content/less/*.less')
        .pipe(less())
        .pipe(gulp.dest('content/css'));
});
Tartaric answered 29/12, 2014 at 12:55 Comment(3)
That doesn't look like a regexp, it looks like a filename wildcard.Mullite
I think it is referred to as a glob in gulp but I am a bit confused by it all.Tartaric
That's what it's called in the shell, too.Mullite
M
44

If Gulp wildcards are like shell wildcards:

content/css/[^_]*.css

The ^ at the beginning of a character set means to match any characters not in the set.

Mullite answered 29/12, 2014 at 12:58 Comment(0)
M
8

You can add a second glob whitch will filter files or folders with an underscore.

For ignore folders I use:

gulp.src(['./src/**/*.html', '!**/_*/**'])

'!**/_*/**' will filter folders that start with an underscore.

Mobcap answered 6/12, 2015 at 16:2 Comment(0)
F
2

Use this plugin https://github.com/robrich/gulp-ignore

var gulpIgnore = require('gulp-ignore');
var condition = '_*.css'; //exclude condition

gulp.task('less', function() {
  gulp.src('content/less/*.less')
    .pipe(gulpIgnore.exclude(condition))
    .pipe(less())
    .pipe(gulp.dest('/dist/'));
});

I think gulp supports file glob not full-regex, But You can give a try to content/css/[^_]*.less

Funnel answered 29/12, 2014 at 13:10 Comment(2)
I'm very open to your new suggestiong but can you check my updated question where I show the code that's being used. I think my version might be a bit simpler as it does not need gulp-ignore. I welcome your comments on this. ThanksTartaric
Check the updated post, But your code is including all the files ending with .less I am not sure if gulp by default supports full regex in filenames!!Funnel
R
1

You could use this regex.

content/css/[^_].*\.css

Add $ at the last if necessary.

Rand answered 29/12, 2014 at 13:0 Comment(2)
Can you tell me what the ".*\" in the middle does here? Also I am looking for files that do not start with an underscore.Tartaric
[^_] matches the first letter of the filename, only if it's not an underscore. So the following .* matches any character non-greedily, it matches underscores also. And the final .css asserts that the filenames must end with .css`Rand
I
1

For those using Python (coming from Google like me), the correct would be:

import glob
glob.glob('content/css/[!_]*.css')
Iranian answered 12/10, 2021 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.