如何用一个脚本处理多个文件?

How to process several files with one script?

您好,我正在使用 Nuance 进行一些关于语音到文本的实验,为此我正在使用节点创建请求,如下所示:

我的凭据:

var Nuance = require("nuance");
var nuance = new Nuance("mycredentials", "mypass");

然后我发送请求如下:

nuance.sendDictationRequest({
    "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
    "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
    "path": "12min.amr", //The path to the file you would like to send to Nuance.
    "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
    "success": function(resp){ //The success callback function.
        console.log(resp);
    },
    "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
        console.log("An error was occurred.");
        console.log(resp);
    }
});

这种方式的问题是我需要使用多个调用来更改这部分:

"path": "12min.amr", //The path to the file you would like to send to Nuance.

由于我在终端中执行请求如下:

node call12.js

然后我在终端上得到结果。

我试过了:

nuance.sendDictationRequest({
    "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
    "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
    "path": "4min.amr", //The path to the file you would like to send to Nuance.
    "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
    "success": function(resp){ //The success callback function.
        console.log(resp);
    },
    "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
        console.log("An error was occurred.");
        console.log(resp);
    }
});


nuance.sendDictationRequest({
    "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
    "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
    "path": "2min.amr ", //The path to the file you would like to send to Nuance.
    "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
    "success": function(resp){ //The success callback function.
        console.log(resp);
    },
    "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
        console.log("An error was occurred.");
        console.log(resp);
    }
});

为了只处理 2 个对话,我正在重复代码,因为我认为这不是最佳方式。 为了处理我的所有文件:

2min.amr 4min.amr 8min.amr

我想知道如何创建一个 for 以便在一个脚本中处理更多文件,所以我真的很想感谢支持来完成这项任务。

const fs = require('fs');
var fileNames = ['2min.amr', '4min.amr', '8min.amr'];

fileNames.forEach((item) => {
    nuance.sendDictationRequest({
        "identifier": "randomIdentifierStringHere", //The user identifier (please refer to Nuance's documentation for more info).
        "language": "es_MX", //The language code (please refer to Nuance's documentation for more info).
        "path": item, //The path to the file you would like to send to Nuance.
        "additionalHeaders": {}, //If you'd like to supply more headers or replace the default headers, supply them here.
        "success": function(resp){ //The success callback function.
            fs.writeFile("my_text.txt", resp, err => {
                if (err) throw err;
                console.log('The file has been saved!');
            });
        },
        "error": function(resp){ //The error callback function - returns the response from Nuance that you can debug.
            console.log("An error was occurred.");
            console.log(resp);
        }
    })
});