windows 批处理 - ECHO 正在更改 %ERRORLEVEL%

windows batch - ECHO is changing the %ERRORLEVEL%

我有以下 .bat 片段

echo about to start echoing, Errorlevel=%Errorlevel%

echo. >> ../time.txt

echo Just echod a new line into time.txt, Errorlevel=%Errorlevel%

echo | set/p=start=%time%, >> ../time.txt    

echo about to start with emake, Errorlevel=%ERRORLEVEL%

输出为:

about to start echoing, Errorlevel=0
Just echod a new line into time.txt, Errorlevel=0
about to start with emake, Errorlevel=1

为什么行 echo | set/p=start=%time%, >> ../time.txt 改变了 %ERRORLVEL%

该行将 ERRORLEVEL 设置为 1,因为 SET /P 每当设置值失败时都会失败(将 ERRORLEVEL 设置为 1),而您的 set/p=start=%time%, 永远不会设置值。第一个 = 之后的所有内容都被视为提示,因此没有变量,命令将始终 "fail".

我不知道你想用那条线做什么,所以我不能给你一个解决方案。但是您应该注意一件事 - SET /P is useless for setting variables if used within a pipe.