在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
SET /P中被剥离空白的解决方案:
窍门是你可以在DOS的文本编辑器EDIT中调用的退格字符。要在EDIT中创建它,请按ctrl +ctrlH。 我想把它粘贴在这里,但是这个网页不能显示它。它在记事本上是可见的(它很奇怪,就像一个小的黑色矩形,中间有一个白圈)
你可以这样写:
<nul set /p=.9 Hello everyone
点可以是任何字符,它只是告诉SET /P文本从这里开始,在空格之前,而不是在“Hello”处。 “9”是一个退格字符的表示,我不能在这里显示。你必须用它来代替9,它就会删除",之后你会得到:
Hello Everyone
而不是:
Hello Everyone
我希望这对你们有帮助
其他回答
SET /P中被剥离空白的解决方案:
窍门是你可以在DOS的文本编辑器EDIT中调用的退格字符。要在EDIT中创建它,请按ctrl +ctrlH。 我想把它粘贴在这里,但是这个网页不能显示它。它在记事本上是可见的(它很奇怪,就像一个小的黑色矩形,中间有一个白圈)
你可以这样写:
<nul set /p=.9 Hello everyone
点可以是任何字符,它只是告诉SET /P文本从这里开始,在空格之前,而不是在“Hello”处。 “9”是一个退格字符的表示,我不能在这里显示。你必须用它来代替9,它就会删除",之后你会得到:
Hello Everyone
而不是:
Hello Everyone
我希望这对你们有帮助
这里是另一个方法,它使用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
我根据@arnep的想法做了一个函数:
echo|set /p="Hello World"
下面就是:
:SL (sameline)
echo|set /p=%1
exit /b
使用call:SL "Hello There" 我知道这没什么特别的,但我花了很长时间才想出来,所以我想我应该把它贴在这里。
我发现这个简单的单行批处理文件“EchoPart.bat”非常有用。
@echo | set /p=%*
然后,我可以在交互式CMD行中,或者作为快捷方式的一部分,写如下所示的内容。它带来了一些新的可能性。
echopart "Hello, " & echopart "and then " & echo Goodbye
如果你在批处理文件中使用它,文本可以从参数变量而不是不可变字符串中获得。例如:
@echopart Hello %* & @echo , how are you?
因此,在"SayHello.bat"中执行这一行允许:
甚至……
玩个游戏,玩得开心!
Late answer here, but for anyone who needs to write special characters to a single line who find dbenham's answer to be about 80 lines too long and whose scripts may break (perhaps due to user-input) under the limitations of simply using set /p, it's probably easiest to just to pair your .bat or .cmd with a compiled C++ or C-language executable and then just cout or printf the characters. This will also allow you to easily write multiple times to one line if you're showing a sort of progress bar or something using characters, as OP apparently was.