gpt4 book ai didi

node.js - 对于某些 Windows 命令,nodejs exec 命令失败,并且没有明确的错误消息

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:55 25 4
gpt4 key购买 nike

我的程序中有一个编辑器,可以动态编写命令并执行它们。我想通过 child_process exec 将 myPublish 目录中的所有文件和文件夹移动到当前目录。我在 Windows 中使用 robocopy 命令。当我在 cmd 中测试 robocopy 时,它工作正常:

robocopy /s .\myPublish .\ /move

但是在程序中,nodejs给出了一条不清楚的错误消息,只是说:“命令失败:robocopy/s .\myPublish .\/move\n”

Command failed for robocopy

最佳答案

我也刚刚遇到这个问题。虽然大多数控制台应用程序在没有错误时应返回退出代码为零,但 robocopy 有一系列自定义退出代码。这使得 Node 认为执行过程中出现了错误,而实际上可能没有错误。

根据 here ,Robocopy 具有以下组成退出代码的退出代码位:

0×10 Serious error. Robocopy did not copy any files. This is either a usage error or an error due to insufficient access privileges on the source or destination directories.

0×08 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.

0×04 Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.

0×02 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.

0×01 One or more files were copied successfully (that is, new files have arrived).

0×00 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.

要解决此问题,您需要捕获错误并检查错误代码:

const cp = require('child_process');
try {
cp.execSync('robocopy ...');
} catch (err) {
console.log(err.status); // get the return code
console.log(err.output.toString()); // get robocopy's full output
}

我认为您通常会认为大于 8 的代码是更严重的错误。

关于node.js - 对于某些 Windows 命令,nodejs exec 命令失败,并且没有明确的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52736919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com