如何将节点包中的函数应用于目录中的所有文件?
How to apply a function from node package to all the files in a directory?
我已经安装了 mammoth.js 将 docx 转换为 html 的模块。我可以将它与单个文件一起使用。
如何对特定文件夹中的所有文件使用相同的模块?我正在尝试保存输出 html 文件,同时保留原始名称(当然不是扩展名)。可能我需要一些其他的包......
以下代码适用于所需目录中的单个文件:
var mammoth = require("mammoth");
mammoth.convertToHtml({path: "input/first.docx"}).then(function (resultObject) {
console.log('mammoth result', resultObject.value);
});
系统为win64
像这样的东西应该可以工作
const fs = require('fs')
const path = require('path')
const mammoth = require('mammoth')
fs.readdir('input/', (err, files) => {
files.forEach(file => {
if (path.extname(file) === '.docx') {
// If its a docx file
mammoth
.convertToHtml({ path: `input/${file}` })
.then(function(resultObject) {
// Now get the basename of the filename
const filename = path.basename(file)
// Replace output/ with where you want the file
fs.writeFile(`output/${filename}.html`, resultObject.value, function (err) {
if (err) return console.log(err);
});
})
}
})
})
我已经安装了 mammoth.js 将 docx 转换为 html 的模块。我可以将它与单个文件一起使用。
如何对特定文件夹中的所有文件使用相同的模块?我正在尝试保存输出 html 文件,同时保留原始名称(当然不是扩展名)。可能我需要一些其他的包...... 以下代码适用于所需目录中的单个文件:
var mammoth = require("mammoth");
mammoth.convertToHtml({path: "input/first.docx"}).then(function (resultObject) {
console.log('mammoth result', resultObject.value);
});
系统为win64
像这样的东西应该可以工作
const fs = require('fs')
const path = require('path')
const mammoth = require('mammoth')
fs.readdir('input/', (err, files) => {
files.forEach(file => {
if (path.extname(file) === '.docx') {
// If its a docx file
mammoth
.convertToHtml({ path: `input/${file}` })
.then(function(resultObject) {
// Now get the basename of the filename
const filename = path.basename(file)
// Replace output/ with where you want the file
fs.writeFile(`output/${filename}.html`, resultObject.value, function (err) {
if (err) return console.log(err);
});
})
}
})
})