Javascript 如何将节点包中的函数应用于目录中的所有文件?
我已经安装了mammoth.js模块,它可以将docx转换为html。我可以将其用于单个文件 如何对特定文件夹中的所有文件使用相同的模块?我试图保存一个输出html文件,同时保留原始名称(当然不是扩展名)。可能我需要一些其他的包裹。。。 以下代码适用于所需目录中的单个文件:Javascript 如何将节点包中的函数应用于目录中的所有文件?,javascript,node.js,batch-processing,mammoth,Javascript,Node.js,Batch Processing,Mammoth,我已经安装了mammoth.js模块,它可以将docx转换为html。我可以将其用于单个文件 如何对特定文件夹中的所有文件使用相同的模块?我试图保存一个输出html文件,同时保留原始名称(当然不是扩展名)。可能我需要一些其他的包裹。。。 以下代码适用于所需目录中的单个文件: var mammoth = require("mammoth"); mammoth.convertToHtml({path: "input/first.docx"}).then(function (resultOb
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);
});
})
}
})
})
像这样的东西应该有用
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);
});
})
}
})
})
使用
fs
模块读取目录,它将为您提供所有文件名的列表。然后迭代这些文件。使用fs
模块读取目录,它将为您提供所有文件名的列表。然后迭代这些文件。就像一个符咒,许多thanx m8!我本来打算用write-to-file部分发帖的,但是你做得更快了::------)不用担心!很高兴它有帮助!像一个魅力工程,许多thanx m8!我本来打算用write-to-file部分发帖的,但是你做得更快了::------)不用担心!很高兴它有帮助!