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

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

问题描述

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

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

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

 系统找不到指定的路径
/ pre>

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

  echo%errorlevel%
0

但是命令不成功!



我注意到,该窗口没有设置%errorlevel%最后一个命令如果你使用重定向..



有没有办法?

解决方案

您可以使用以下内容:

  C:\> type nul> C:\does\\\
ot\exist\file.txt&& echo ok || echo fail
系统找不到指定的路径。
fail

C:\> echo%errorlevel%
1

我一直以为&&&和||操作符使用ERRORLEVEL,但显然不是。



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



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

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


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

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

echo %errorlevel%
0

Yet the command was not successful!

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

Is there a way around this?

解决方案

You can use the following:

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

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

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.

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天全站免登陆