简体   繁体   English

对于某些 Windows 命令,nodejs exec 命令失败,没有明确的错误消息

[英]nodejs exec command fails for some windows commands with no clear error message

I have an editor in my program and dynamically write commands and execute them.我的程序中有一个编辑器,可以动态编写命令并执行它们。 I want to move all files and folders inside myPublish directory to current directory by child_process exec.我想通过 child_process exec 将 myPublish 目录中的所有文件和文件夹移动到当前目录。 I use robocopy command in windows.我在 Windows 中使用 robocopy 命令。 when I test robocopy in cmd, it works correctly:当我在 cmd 中测试 robocopy 时,它工作正常:

robocopy /s .\myPublish .\ /move

but in program, nodejs gives an unclear error message that just says: "Command failed: robocopy /s .\\myPublish .\\ /move\\n"但在程序中,nodejs 给出了一条不清楚的错误消息,它只是说:“命令失败:robocopy /s .\\myPublish .\\ /move\\n”

robocopy 命令失败

I've just hit this issue also.我也刚碰到这个问题。 While most console applications should return an exit code of zero when there's no errors, robocopy has a series of custom exit codes.虽然大多数控制台应用程序在没有错误时应该返回零退出代码,但 robocopy 有一系列自定义退出代码。 This makes Node think that there's been an error during execution, when there may not have been.这让 Node 认为在执行过程中出现了错误,而实际上可能没有。

As per here , Robocopy has the following exit code bits that make up the exit code:根据此处,Robocopy 具有以下构成退出代码的退出代码位:

0×10 Serious error. 0×10 严重错误。 Robocopy did not copy any files. Robocopy 没有复制任何文件。 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). 0×08 无法复制某些文件或目录(发生复制错误并超出重试限制)。 Check these errors further.进一步检查这些错误。

0×04 Some Mismatched files or directories were detected. 0×04 检测到一些不匹配的文件或目录。 Examine the output log.检查输出日志。 Housekeeping is probably necessary.家政服务可能是必要的。

0×02 Some Extra files or directories were detected. 0×02 检测到一些额外的文件或目录。 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×01 一个或多个文件复制成功(即有新文件到达)。

0×00 No errors occurred, and no copying was done. 0×00 未发生错误,未进行复制。 The source and destination directory trees are completely synchronized.源目录树和目标目录树完全同步。

To fix this, you need to catch the error and inspect the error code:要解决此问题,您需要捕获错误并检查错误代码:

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
}

I think you would generally consider a code greater than 8 to be a more serious error.我认为您通常会认为大于 8 的代码是一个更严重的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM