gpt4 book ai didi

windows - 批处理文件 - 检查命令是否可用

转载 作者:可可西里 更新时间:2023-11-01 10:55:47 26 4
gpt4 key购买 nike

我正在 Windows 中编写一个需要运行 nodejs 应用程序的小批处理文件。在运行该应用程序之前,我需要确保该节点已由用户安装,如果没有,则向他显示一条消息,表明该节点是必需的。

我做的是这样的:

@echo OFF

setlocal EnableDelayedExpansion

REM Check if node is installed
for /f "delims=" %%i in ('node -v') do set output=%%i

IF "!output!" EQU "" (
echo node could not be found
) else (
node %~dp0app.js
)

如果用户安装了节点,那么 output 将包含版本号。如果没有安装,那么它将是空的。这个逻辑有效。但是如果没有安装node(node -v command not found),批处理结果也会显示如下输出:

'node' is not recognized as an internal or external command,
operable program or batch file.
node could not be found

我想对用户隐藏“无法识别”消息,只显示“找不到节点”。

如何隐藏它?

最佳答案

要抑制错误消息,将其重定向到 NUL:

set "output=not installed"
for /f "delims=" %%i in ('node -v 2^>nul') do set output=%%i
echo %output%

另一种方式(受 Npocmaka 的回答启发):

where node.exe >nul 2>&1 && echo installed || echo not installed

或者为了更接近您的原始输出:

where node.exe >nul 2>&1 && node %~dp0app.js || echo node could not be found

关于windows - 批处理文件 - 检查命令是否可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34930335/

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