A
A
artem72992018-09-28 18:51:24
gulp.js
artem7299, 2018-09-28 18:51:24

How to give a folder the name of a file/s in it in gulp?

Hello! What you need to do is to have Gulp take the file(s) and move it to a folder that will take the name of the file that was moved into it.
For example, there are three files: "example.ttf", "example.otf" and "example.woff"
You need to configure gulp so that it takes these files and moves them to a folder that will take a name depending on the name of those in it files, in this case, "example"
Can you please tell me if this can be done through gulp at all? And how? I couldn't find anything in google and yandex.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2018-09-28
@artem7299

const Folder = './fonts/';
const fs = require('fs');
const path = require('path');

fs.readdir(Folder, (err, files) => {
  files.forEach(file => {
    let fileName = path.parse(file).name
    if (!fs.existsSync(Folder + fileName)) fs.mkdirSync(Folder + fileName);

    fs.rename(Folder + file, Folder + fileName + '/' + file, function(err) {
      if (err) console.log('ERROR: ' + err);
    });
  });
})

Here is a simple node script that will do it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question