呼应在同一行 [英] Echoing in the same line

查看:82
本文介绍了呼应在同一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看看你的分贝的previous问题,我dis't尝试一个答案,但我试试。

I had a look at the previous questions of your db and I dis't try an answer, but I try.

我想编写如下行code:

I would like to write the following lines code:

回声执行备份....

echo Executing backup....

备份程序

回声确定

但输出应该是:

执行备份...好

这是可能的吗?!

推荐答案

我想你使用的是DOS / NT-批次。

I suppose you are using dos/nt-batch.

这是可能的设置/ p命令,因为set / p不打印CRLF

It is possible with the set /p command, because set /p doesn't print a CrLf

set /p "=Executing backup...." <nul
echo OK

也有可能以清除与CR字符的行。
要知道,以一组/ P的前空格字符将被忽略(在Vista中,而不是在XP)是很重要的,所以!CR!一直到后来还是在最后放置。
一条CR只能与delayedExpansion,但不是在延迟扩展阶段被显示,因为%CR%的作品,但是CR字符都在膨胀百分比相中除去(或直接在此阶段之后)。

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.

其中只使用一个线显示计数的例子

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

编辑:解释,如何变量 CR 创建(一招完成)

Explanation, how the variable cr is created (Done with a trick)

设置变量 X 来单点(字符本身并不重要),它是反复成为8188字符由/ 的办法后, !期L %% C(1,1,13)DO设置X = X:〜0,4094 !! X:!〜0,4094

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!

然后变量,两个空间和一个都CR和LF被返回到一个文件中以回音!到X! &GT; %TEMP%\\ cr.tmp (注意之间的两个空格X &GT;!与自然行结尾呼应赔罪内部)

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)

我们现在有8192个字符,但数据缓冲区只能容纳8191个字符,所以最后一个字符(换行)将被丢弃!

We now have 8192 characters, but the data buffer can only hold 8191 characters, so the last character (the linefeed) will be dropped!

在下一行回声\\&GT;&GT; %TEMP%\\ cr.tmp ,另一个 CR / LF 集被附加到文件( \\ 在命令仅仅是输出什么吧回车和换行,如回声由它的自我将输出 ECHO为ON /关闭),这一点很重要,作为一个单一的 CR 不能在一行的末尾被读取(稍后)。

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).

所以文件现在包含&LT; 8188的方式&gt;&lt;空&GT;&lt;空&GT;&LT; CR&GT;&LT; CR&GT;&LT; LF&GT;

FOR / F令牌= 2有usebackq%%一中(%temp%\\ cr.tmp)做读取第二道理,delimters是标准的空间和标签,所以第二个令牌仅一个 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.

最后, ENDLOCAL 用于返回到一个环境,而临时变量 X C A 现有的(与 ENDLOCAL 在括号中,它允许设置的 CR ENDLOCAL 其实需要在括号内的最后影响(也可以写成 FOR / F令牌= 2有usebackq%%一中(%temp%\\ cr.tmp)做的endlocal和放大器;设置CR = %% A和GOTO:EOF

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)

此外

这是创建一个CR字符我第一种方式,但它需要一定的时间和临时文件。结果
后来我看到从拷贝/ Z 命令检索CR更为简单的方法。

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.

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

这篇关于呼应在同一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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