文件重定向在Windows和%ERRORLEVEL% [英] File redirection in Windows and %errorlevel%

查看:143
本文介绍了文件重定向在Windows和%ERRORLEVEL%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们希望在Windows中创建一个空文件使用以下命令:

Lets say we want to create an empty file in windows with the following command:

type nul > C:\does\not\exist\file.txt

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

the directory does not exist, so we get the error:

The system cannot find the path specified

如果您打印出%ERRORLEVEL%的输出是:

If you print out the %errorlevel% the output is:

echo %errorlevel%
0

然而,命令没有成功!

Yet the command was not successful!

我注意到,Windows不设置%ERRORLEVEL%最后一个命令,如果你使用重定向。

I noticed, that windows does not set the %errorlevel% of the last command if you use redirection..

有没有办法解决?

推荐答案

您可以使用以下

C:\>type nul > C:\does\not\exist\file.txt && echo ok || echo fail
The system cannot find the path specified.
fail

C:\>echo %errorlevel%
1

我总是假设和放大器;&安培;和||运营商使用的错误级别,但不apparantly

I always assumed the && and || operators used ERRORLEVEL, but apparantly not.

很好奇那个ErrorLevel被重定向错误后,只有当您使用设置||运营商。我决不会猜到。也我会永远都不屑于测试,如果不是你非常好的问题。

Very curious that ERRORLEVEL is set after redirection error only if you use the || operator. I never would have guessed. Nor would I ever have bothered to test if not for your excellent question.

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

If all you want to do is set the ERRORLEVEL upon redirection failure, then of course you can simply do:

type nul > C:\does\not\exist\file.txt || rem

这篇关于文件重定向在Windows和%ERRORLEVEL%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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