加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows中的文件重定向和%errorlevel%

发布时间:2020-12-13 21:07:45 所属栏目:Windows 来源:网络整理
导读:假设我们要在Windows中使用以下命令创建一个空文件: type nul C:doesnotexistfile.txt 该目录不存在,所以我们得到错误: The system cannot find the path specified 如果打印出%errorlevel%,输出为: echo %errorlevel%0 然而命令不成功! 我注意
假设我们要在Windows中使用以下命令创建一个空文件:
type nul > C:doesnotexistfile.txt

该目录不存在,所以我们得到错误:

The system cannot find the path specified

如果打印出%errorlevel%,输出为:

echo %errorlevel%
0

然而命令不成功!

我注意到,如果您使用重定向,则Windows不会设置上一个命令的%errorlevel%。

有没有解决的办法?

您可以使用以下内容
C:&;type nul > C:doesnotexistfile.txt && echo ok || echo fail
The system cannot find the path specified.
fail

C:&;echo %errorlevel%
1

我一直以为&&和||运算符使用ERRORLEVEL,但不可见。

非常好奇,只有当您使用||时,才会在重定向错误后设置ERRORLEVEL操作符。我从来没有猜到如果不是你的优秀问题,我也不会打扰测试。

如果您想要做的是在重定向失败时设置ERRORLEVEL,那么您当然可以简单地做:

type nul > C:doesnotexistfile.txt || rem

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读