在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
DIY cw.exe(控制台编写)实用程序
如果你找不到现成的,你可以自己动手做。有了这个cw实用程序,你可以使用每一种字符。至少,我是这么想的。请对它进行压力测试,然后告诉我。
工具
你所需要的只是安装。net,这在当今非常普遍。
材料
输入/复制粘贴了一些字符。
步骤
用以下内容创建.bat文件。
/* >nul 2>&1
@echo off
setlocal
set exe=cw
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*csc.exe"') do set "csc=%%v"
"%csc%" -nologo -out:"%exe%.exe" "%~f0"
endlocal
exit /b %errorlevel%
*/
using System;
namespace cw {
class Program {
static void Main() {
var exe = Environment.GetCommandLineArgs()[0];
var rawCmd = Environment.CommandLine;
var line = rawCmd.Remove(rawCmd.IndexOf(exe),exe.Length).TrimStart('"');
line = line.Length < 2 ? "\r" : line.Substring(2) ;
Console.Write(line);
}
}
}
运行它。 现在您有了一个很棒的4KB实用程序,可以删除.bat。
或者,您可以在任何批处理中插入此代码作为子例程,将生成的.exe发送到%temp%,在批处理中使用它,并在完成时删除它。
如何使用
如果你想写一些没有换行的东西: 任何你想要的,即使是"",但记住转义^|,^^,^&等,除非双引号,如"| ^& "。
如果你想要一个回车(回到行首),运行just 连续波
所以试试从命令行:
for /l %a in (1,1,1000) do @(cw ^|&cw&cw /&cw&cw -&cw&cw \&cw)
其他回答
你可以从gnuwin32 (coreutils包)中使用“tr”删除换行符
@echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause
顺便说一下,如果您正在编写大量脚本,gnuwin32是一个金矿。
可以使用set /p命令禁用新行。set /p命令不识别空格,因此可以使用点和退格字符使其识别空格。您还可以使用变量作为内存,并将想要打印的内容存储在其中,这样您就可以打印变量而不是语句。例如:
@echo off
setlocal enabledelayedexpansion
for /f %%a in ('"prompt $H & for %%b in (1) do rem"') do (set "bs=%%a")
cls
set "var=Hello World! :)"
set "x=0"
:loop
set "display=!var:~%x%,1!"
<nul set /p "print=.%bs%%display%"
ping -n 1 localhost >nul
set /a "x=%x% + 1"
if "!var:~%x%,1!" == "" goto end
goto loop
:end
echo.
pause
exit
通过这种方式,您可以打印任何内容而无需新行。我已经使程序打印字符一个接一个,但你也可以使用文字,而不是通过改变循环字符。
在上面的例子中,我使用了" enabledelayeexpand ",所以set /p命令不识别"!"字符,并打印一个点来代替它。我希望你不要用感叹号“!”;)
从这里
<nul set /p =Testing testing
同时也从空间的使用开始呼应
echo.Message goes here
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.
jscript:
@if (@X)==(@Y) @end /*
@cscript //E:JScript //nologo "%~nx0" %*
@exit /b %errorlevel%
*/if(WScript.Arguments.Count()>0) WScript.StdOut.Write(WScript.Arguments.Item(0));
如果它叫write.bat,你可以这样测试它:
call write.bat string & echo _Another_String_
如果你想使用powershell,但有cmd定义的变量,你可以使用:
set str=_My_StrinG_
powershell "Write-Host -NoNewline ""%str%"""" & echo #Another#STRING#