Multiple files pipeline on Windows command line

Recently I switched my building process from Grunt to pure command line tools/commands, taking advantage of the less known npm-scripts feature.

I was actually motivated by this post by Keith Cirkel, followed by a second, incredibly useful post exposing some common tasks made entirely from command line commands.

However, K. Cirkel didn't examine a pretty common case for CSS processing: multiple files pipelines. Infact, if you read his second post you see he nicely shows how to prefix and minify a single CSS file with autoprefixer and cssmin, but he doesn't say anything about a (probably more common) building chain with more than one CSS file to process.

Maybe if you work on Linux, you never thought it would be a problem... Well, on Windows it's a bit trickier, but actually not so much.

Here's the magic snippet:

FOR /F "usebackq" %f IN (`dir src\css /b /A:-D`) DO autoprefixer src/css/%f -o - | cssmin > build/css/%f
			

So, we are exploiting the Batch FOR command to iterate over the files in src/css, and then feed them to autoprefixer and cssmin.

The options /F and "usebackq" are needed to make FOR execute the command after IN before evaluating the expression as an iterable collection.

The options /b and /A:-D passed to dir make the command print only the filenames, without sub-directories.

And that's all folks! You can check other handy scripts here and here.