windows 回响在同一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4097041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Echoing in the same line
提问by jeb
I had a look at the previous questions of your db and I didn't try an answer, but I try.
我看了你的数据库之前的问题,我没有尝试回答,但我尝试了。
I would like to write the following lines code:
我想写以下几行代码:
echo Executing backup....
backup procedure
echo Ok
but the output should be:
但输出应该是:
Executing backup... Ok
That's possible?!
这可能吗?!
回答by jeb
I suppose you are using dos/nt-batch.
我想您正在使用 dos/nt-batch。
It is possible with the set /p command, because set /p doesn't print a CrLf
可以使用 set /p 命令,因为 set /p 不打印 CrLf
set /p "=Executing backup...." <nul
echo OK
Also it's possible to erase the line with a CR character. It's important to know that whitespace characters at the front of an set /p are ignored (in Vista, not in XP), so the !cr! has to placed later or at the end. A CR can only be displayed with delayedExpansion, because %cr% works, but CR characters are removed in the percent expansion phase(or directly after this phase), but not in the delayed expansion phase.
也可以用 CR 字符擦除该行。重要的是要知道 set /p 前面的空白字符会被忽略(在 Vista 中,而不是在 XP 中),因此 !cr! 必须放在后面或最后。CR 只能用delayedExpansion 显示,因为%cr% 有效,但CR 字符在百分比扩展阶段(或直接在此阶段之后)被删除,但在延迟扩展阶段不会。
Example of a counter which use only one line for displaying
仅使用一行显示的计数器示例
@echo off
setlocal EnableDelayedExpansion EnableExtensions
call :CreateCR
for /l %%n in (1,1,10000) do (
set /P "=Count %%n!CR!" <nul
)
echo(
goto :eof
:CreateCR
rem setlocal EnableDelayedExpansion EnableExtensions
set "X=."
for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!
echo !X! > %temp%\cr.tmp
echo\>> %temp%\cr.tmp
for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do (
endlocal
set cr=%%a
goto :eof
)
goto :eof
EDIT: Explanation, how the variable cr
is created (Done with a trick)
编辑:解释,如何cr
创建变量(用技巧完成)
After setting variable X
to a single dot (the character itself is unimportant), it is repeated to become 8188 characters by way of for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!
将变量设置X
为单点后(字符本身无关紧要),通过以下方式重复成为8188个字符for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!
Then the variable, two spaces and both a CR and LF are echoed into a file with echo !X! > %temp%\cr.tmp
(Notice the two spaces between the !X!
and the >
and the natural line endings echo amends internally)
然后变量,两个空格以及一个 CR 和 LF 被回显到一个文件中echo !X! > %temp%\cr.tmp
(注意!X!
和之间的两个空格>
和自然行结尾 echo 在内部修改)
We now have 8192 characters, but the data buffer can only hold 8191 characters, so the last character (the linefeed) will be dropped!
我们现在有 8192 个字符,但是数据缓冲区只能容纳 8191 个字符,所以最后一个字符(换行符)将被删除!
In the next line echo\>> %temp%\cr.tmp
, another CR/LF
set is appended to the file (the \
in the command is just to output nothing bar the carriage return and line feed, as echo
by it's self will output ECHO is ON/OFF
), that's important, as a single CR
can't be read at the end of a line (More later).
在下一行中echo\>> %temp%\cr.tmp
,另一个CR/LF
集合被附加到文件中(\
命令中只是不输出任何东西,回车和换行,因为echo
它自己会输出ECHO is ON/OFF
),这很重要,因为CR
在一行的结尾(稍后会详细介绍)。
So the file now contains <8188 .'s><SPACE><SPACE><CR><CR><LF>
所以文件现在包含 <8188 .'s><SPACE><SPACE><CR><CR><LF>
The for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do
reads the second token, the delimters are standard space and tab, so the second token is only a single CR
, as the following CR/LF
is removed as standard line ending.
在for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do
读取第二令牌时,delimters是标准的空间和标签上,所以第二令牌是只有一个CR
,如下CR/LF
被作为标准的行结束除去。
Finally the endlocal
is used to return to an environment without the temporary variables X
, c
and a
existing (As with the endlocal
in brackets, it allows the setting of cr
before the endlocal
actually takes affect at the end of the brackets (could also be written as for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do endlocal&set cr=%%a&goto :eof
)
最后endlocal
用于返回一个没有临时变量的环境X
,c
并且a
存在(与endlocal
括号一样,它允许设置在括号末尾实际生效cr
之前endlocal
(也可以写为for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do endlocal&set cr=%%a&goto :eof
)
Additionally
此外
This was my first way to create a CR character, but it needs some time and a temporary file.
Later I saw a simpler method of retrieving the CR from a copy /z
command.
这是我创建 CR 角色的第一种方法,但它需要一些时间和一个临时文件。
后来我看到了一种从copy /z
命令中检索 CR 的更简单的方法。
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
回答by Piotr Czapla
Try this on Posix system (Linux)
在 Posix 系统 (Linux) 上试试这个
echo -n "Executing backup.... "
echo -n "backup procedure "
echo "Ok"
It is much harder on Windows. You will need to use something like this:
在 Windows 上要困难得多。你将需要使用这样的东西:
@echo off
echo|set /p ="Executing backup...."
echo|set /p =" backup procedure"
Check this post: http://www.pcreview.co.uk/forums/showpost.php?s=1a20b16775d915998b30bd76a0ec5d35&p=4432915&postcount=7.
检查这个帖子:http: //www.pcreview.co.uk/forums/showpost.php?s=1a20b16775d915998b30bd76a0ec5d35& p=4432915& postcount=7。
回答by Gary Rowe
It's a bit of a hack, but here is an article describing how to do itfor Windows.
这有点像黑客,但这里有一篇文章描述了如何为 Windows做到这一点。
From the article, the final result (edited for your setup) looks like this:
从文章中,最终结果(针对您的设置进行了编辑)如下所示:
SET /P var=Backing up
%Result%....<NUL
Backup_process %Result% >NUL 2>&1
IF ERRORLEVEL 1
ECHO FAIL
ELSE
ECHO OK
回答by aphoria
I've done something similar using a VBScript.
我使用 VBScript 做过类似的事情。
Put this code in EchoNoNewline.vbs:
将此代码放在EchoNoNewline.vbs 中:
If WScript.Arguments.Named.Exists("TEXT") Then
WScript.StdOut.Write WScript.Arguments.Named.Item("TEXT")
End If
From your batch file, use the script like this:
从您的批处理文件中,使用如下脚本:
CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Executing backup...."
backup procedure
CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Ok"
回答by George Birbilis
at What does a forward slash before a pipe in cmd do to remove the line ending of an echo?the best suggestion is:
在cmd 中的管道之前的正斜杠如何删除回声的行尾?最好的建议是:
to echo text without a linefeed is very inefficient, as a pipe creates two new instances of cmd.exe. It's much simpler and faster to use
在没有换行符的情况下回显文本效率非常低,因为管道会创建 cmd.exe 的两个新实例。使用起来更加简单快捷
<nul set /p "=My Text"
The redirect from NUL will also stop the waiting for user input.
来自 NUL 的重定向也将停止等待用户输入。