对于某些Windows命令,nodejs exec命令失败,并且没有明确的错误消息 [英] nodejs exec command fails for some windows commands with no clear error message

查看:168
本文介绍了对于某些Windows命令,nodejs exec命令失败,并且没有明确的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. I use robocopy command in windows. when I test robocopy in cmd, it works correctly:

robocopy /s .\myPublish .\ /move

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

but in program, nodejs gives an unclear error message that just says: "Command failed: robocopy /s .\myPublish .\ /move\n"

推荐答案

我也遇到了这个问题.尽管大多数控制台应用程序在没有错误的情况下应返回零退出代码,但robocopy具有一系列自定义退出代码.这使Node认为执行期间可能有错误,而可能没有.

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. This makes Node think that there's been an error during execution, when there may not have been.

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

As per here, Robocopy has the following exit code bits that make up the exit code:

0×10严重错误.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无法复制某些文件或目录(发生复制错误并且超出了重试限制).进一步检查这些错误.

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检测到一些不匹配的文件或目录.检查输出日志.客房清洁可能是必要的.

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

0×02检测到某些Extra文件或目录.检查输出日志.可能需要一些客房清洁服务.

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

0×01成功复制了一个或多个文件(即,新文件已到达).

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

0×00没有发生错误,也没有复制.源目录树和目标目录树完全同步.

0×00 No errors occurred, and no copying was done. 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
}

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

I think you would generally consider a code greater than 8 to be a more serious error.

这篇关于对于某些Windows命令,nodejs exec命令失败,并且没有明确的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆