在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
这里是另一个方法,它使用Powershell Write-Host,它有一个-NoNewLine参数,结合start /b,它从批处理中提供了相同的功能。
NoNewLines.cmd
@ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - ';Write-Host -NoNewLine 'Result 2 - ';Write-Host -NoNewLine 'Result 3 - '"
PAUSE
输出
Result 1 - Result 2 - Result 3 - Press any key to continue . . .
下面这个略有不同,并不完全像OP想要的那样工作,但很有趣,因为每个结果都覆盖了模拟计数器的前一个结果。
@ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 2 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 3 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 4 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 5 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 6 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 7 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 8 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 9 - '"
PAUSE
其他回答
作为@xmechanix回答的补充,通过将内容写入一个文件,我注意到:
echo | set /p dummyName=Hello World > somefile.txt
这将在打印的字符串的末尾添加一个额外的空格,这可能是不方便的,特别是因为我们试图避免在字符串的末尾添加一个新行(另一个空白字符)。
幸运的是,引用要打印的字符串,即使用:
echo | set /p dummyName="Hello World" > somefile.txt
将打印结尾不带换行符或空格字符的字符串。
我相信没有这样的选择。或者你可以试试这个
set text=Hello
set text=%text% world
echo %text%
从这里
<nul set /p =Testing testing
同时也从空间的使用开始呼应
echo.Message goes here
我发现这个简单的单行批处理文件“EchoPart.bat”非常有用。
@echo | set /p=%*
然后,我可以在交互式CMD行中,或者作为快捷方式的一部分,写如下所示的内容。它带来了一些新的可能性。
echopart "Hello, " & echopart "and then " & echo Goodbye
如果你在批处理文件中使用它,文本可以从参数变量而不是不可变字符串中获得。例如:
@echopart Hello %* & @echo , how are you?
因此,在"SayHello.bat"中执行这一行允许:
甚至……
玩个游戏,玩得开心!
受到这个问题答案的启发,我制作了一个简单的计数器批处理脚本,在同一行上打印进度值(0-100%)(覆盖前一行)。也许这对寻找类似解决方案的其他人也有价值。
注:*为不可打印字符,应使用[Alt + Numpad 0 + Numpad 8]组合键输入,即退格字符。
@ECHO OFF
FOR /L %%A in (0, 10, 100) DO (
ECHO|SET /P="****%%A%%"
CALL:Wait 1
)
GOTO:EOF
:Wait
SET /A "delay=%~1+1"
CALL PING 127.0.0.1 -n %delay% > NUL
GOTO:EOF