最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS copy only certain files - Stack Overflow

programmeradmin0浏览0评论

I have an input directory like this:

resouces
├── a.avi
├── b.mp3
├── c.pdf
├── d.png
└── ...

I'm trying to generate the following one:

resouces
├── audio
    └── *.mp3
├── video
    └── *.avi
└── ...

I'm using fs-extra npm module. This is my code:

fse.ensureDir(resourcesOutputDirectory, (error) => {
    if (error) {
        console.err("An error ocurred creating the resources directory " + error.message);
    } else {
        fse.copy(resourcesInputDirectory, resourcesOutputDirectory, "/**/*.mp3", (err) => {
            if (err) {
                console.err("An error ocurred moving resource directory to XML exported directory " + err.message);
            } else {
                console.log("Files has been succesfully copied");
            }
         });
     }
});

I don't know how to correctly use the filter option (third argument in copy call) to copy only certain files to my output directory.

Thank in advance!

I have an input directory like this:

resouces
├── a.avi
├── b.mp3
├── c.pdf
├── d.png
└── ...

I'm trying to generate the following one:

resouces
├── audio
    └── *.mp3
├── video
    └── *.avi
└── ...

I'm using fs-extra npm module. This is my code:

fse.ensureDir(resourcesOutputDirectory, (error) => {
    if (error) {
        console.err("An error ocurred creating the resources directory " + error.message);
    } else {
        fse.copy(resourcesInputDirectory, resourcesOutputDirectory, "/**/*.mp3", (err) => {
            if (err) {
                console.err("An error ocurred moving resource directory to XML exported directory " + err.message);
            } else {
                console.log("Files has been succesfully copied");
            }
         });
     }
});

I don't know how to correctly use the filter option (third argument in copy call) to copy only certain files to my output directory.

Thank in advance!

Share Improve this question asked Oct 24, 2016 at 8:37 AbelAbel 3559 silver badges20 bronze badges 1
  • 1 Reading fse documentation lead me to believe, althought the exemple in the doc is obviously wrong, that instead of "/**/*.mp3", you are supposed to put a function, that return true when you want to copy the file, and false when not. Like (file) => {return (file.endsWith('.mp3');} – DrakaSAN Commented Oct 24, 2016 at 8:51
Add a ment  | 

3 Answers 3

Reset to default 2

Looking at the docs it doesn't look as if you can use globs (as you are doing in your example). You can either use a function that returns a boolean, or a regular expression.

For instance, if you want to match all mp3s or avis then you can do;

fse.copy(inDir, outDir, /.*(.mp3|.avi)$/, (err) => { })

Use this gist to selectively copy files from a folder and provide specific output name

const fs = require('fs-extra');
const folders = [
  {
    path: './build/static/css',
    regex: /^(main).[\w]*.css$/,
    outputFile: '../public-pages/style/sme.css'
  },
  {
    path: './build/static/js',
    regex: /^(main).[\w]*.js$/,
    outputFile: '../public-pages/js/sme.js'
  }
];
/*
following code will copy files from ./build/static/css matching the regex pattern to new location specified by output file
*/
folders.forEach(folder => {
  fs.readdirSync(folder.path).forEach(file => {
    if (folder.regex.test(file)) {
      console.log(`copying file ${file}`);
      fs.copy(`${folder.path}/${file}`, folder.outputFile)
        .then(() => console.log('success!'))
        .catch(err => console.error(err));
    }
  });
});

Finally I got it:

As @Jivings said, I just need a regular expression as filter. But, the problem is that copy method from fse uses ncp, and it seems to have a bug with filter right now. Using copySync works.

发布评论

评论列表(0)

  1. 暂无评论