admin管理员组文章数量:1430358
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 returntrue
when you want to copy the file, andfalse
when not. Like(file) => {return (file.endsWith('.mp3');}
– DrakaSAN Commented Oct 24, 2016 at 8:51
3 Answers
Reset to default 2Looking 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.
本文标签: javascriptNode JS copy only certain filesStack Overflow
版权声明:本文标题:javascript - Node JS copy only certain files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745516730a2661585.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论