问题描述
动机
我有一个第三方,为某种特定功能编写的.bat文件要花多长时间,并且将花费相当多的精力来重写(这种工作也被我的问题所阻碍).在for循环中,最基本的调试方法似乎是在屏幕上回显一些信息.我以前使用其他语言中的\r
(0x0D)字符来执行此操作,在某些终端/控制台上,该字符会重新写入同一行(为避免溢出,因为在我的情况下,最后一行将包含错误).我已经将值保存到变量中.但是,由于迭代可能要花很长时间,所以我仍然很乐意将一些输出写到屏幕上,不会溢出.
I have a 3rd party, somehow long .bat file written for some specific function and would take considerable effort to re-write (which effort is also hindered by my problem). In for loops the most basic way to debug it would seem echoing some information to the screen. I used to do this with \r
(0x0D) character in other languages that on some terminals/console re-writes the same line (to avoid overflooding, since in my case the last line would contain the error). I already save the value to a variable. However, since iteration might take quite long, I'd still be happy to write some output to the screen that won't overflood.
我尝试过的事情
- 我知道我可以用
echo.
在cmd
中回显单个换行符-但是我只需要回车即可 - 我已经尝试过这些,但是它们不起作用:
echo \r
,echo ^r
,echo \x0d
,echo ^x0d
,echo #0d
,echo ^#0d
,echo #x0d
,echo ^x0d
- 我尝试在没有太多帮助的情况下将网塞进去
- I know I can echo a single newline in
cmd
withecho.
- however I need only the carriage return - I've tried these but they did't work:
echo \r
,echo ^r
,echo \x0d
,echo ^x0d
,echo #0d
,echo ^#0d
,echo #x0d
,echo ^x0d
- I've tried to duck the net for similar stuff without much help
问题
是否可以在Windows/DOS/NT/CMD批处理文件中以某种方式回显回车符(或其他不可打印的字符)?
Is it possible to somehow echo a carriage-return (or other non-printable) character in a windows/dos/nt/cmd batch file?
ps.我使用XP或7 cmd处理器
ps. I use the XP or the 7 cmd processor
推荐答案
您需要两次破解-一次是定义回车符,另一次是在不发出换行符的情况下回显一行文本.
You need two hacks - one to define a carriage return character, and another to echo a line of text without issuing the newline character.
1)定义回车.
:: Define CR to contain a carriage return (0x0D)
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"
定义后,只能通过延迟扩展来访问该值-如!CR!
中一样,而不是%CR%
.
Once defined, the value can only be accessed via delayed expansion - as in !CR!
, not %CR%
.
2)在不发布换行符的情况下将文本打印到屏幕上
<nul set /p "=Your message here"
如果字符串以=
开头,则会失败.
This will fail if the string starts with a =
.
此外,根据Windows版本的不同,可能会删除引号和/或空格
Also, leading quotes and/or white space may be stripped, depending on the Windows version
将它们放在一起
@echo off
setlocal enableDelayedExpansion
:: Define CR to contain a carriage return (0x0D)
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"
<nul set/p"=Part 1 - press a key!CR!"
pause >nul
<nul set/p"=Part 2 - press a key!CR!"
pause >nul
<nul set/p"=Part 3 - Finished !CR!"
请注意,我将!CR!
放在每条消息的末尾,为下一条做准备.您不能将!CR!
放在开头,因为前导空格将被删除.
Note that I put the !CR!
at the end of each message in preparation for the next. You cannot put the !CR!
at the beginning because leading white space will be stripped.
这篇关于可以在批处理/cmd中回显一些不可打印的字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!