使用 chokidar 将新文件从一个位置移动到另一个位置以在 nodejs 和 powershell 脚本中进行监控

问题描述

我是 node.js 的新手,正在尝试设置脚本来监视文件夹并将保存在那里的任何文件移动到另一个位置,脚本适用于 1 个文件,但在移动多个文件显示错误,即使它确实移动了它们- 报告第二个和第三个文件丢失。我猜这是由于节点的 async/sync 方法,我可能需要使用回调或承诺,但需要帮助了解如何在此示例中使用。

错误

(node:1812) UnhandledPromiseRejectionWarning: TypeError: callback is 不是函数

(node:1812) UnhandledPromiseRejectionWarning:未处理的承诺 拒绝。此错误源于抛出异步 没有 catch 块的函数,或者通过拒绝一个承诺 不使用 .catch() 处理。在未处理的节点上终止节点进程 承诺拒绝,使用 CLI 标志 --unhandled-rejections=strict (见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (rejection id: 1) (node:1812) [DEP0018] DeprecationWarning: Unhandled 不推荐使用承诺拒绝。在未来,承诺拒绝 未处理的将终止 Node.js 进程 非零退出代码

EOI_HUzDtR-mwMove-Item:找不到路径“C:\test2\test.txt”,因为 它不存在。

代码

const filelocation = ('c:/test/');
const destlocation = ('c:/new_folder/');
const watcher = chokidar.watch(".",{ ignored: /^\./,persistent: true,cwd: filelocation,awaitWriteFinish: true });

watcher.on('add',(path) => {
    var FileName = path;
    logger.info(`File ${FileName} has been added to ${filelocation}`);
    Movefile({param1: FileName,param2: filelocation,param3: destlocation});
});

function Movefile(options) {
    var FileName = options.param1;
    var filelocation = options.param2;
    var destlocation = options.param3;
    logger.info(`Started move for file ${FileName} in location ${filelocation} to location ${destlocation}`);
    ps.addCommand("./move_file.ps1");
    ps.addParameters([{ name: 'file',value: FileName },{ name: 'flocation',value: filelocation },{ name: 'dlocation',value: destlocation }]);
    ps.invoke()
        .then(output => {
            logger.info(`Completed move of file ${FileName}`);
            //ps.dispose();
        })
        .catch(err => {
            logger.error('Move file Failed to execute',err);
            //ps.dispose();
        });
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)