如何清除 Windows 命令行中的最后一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/367971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:44:16  来源:igfitidea点击:

How to clear last line in the Windows command line?

windowscommand-linecmd

提问by Dungeo

Is there a function, that clears last line in command prompt? I dont mean "cls" - it clears the whole sreen, I want to delete just the last line.

是否有一个功能可以清除命令提示符中的最后一行?我的意思不是“cls”——它清除了整个屏幕,我只想删除最后一行。

e.g. I am searching for a file in a folder and its subfolders and I want to print to cmd current folder - but I want to rewrite it, when the folder changes, not just append to the end. I know, this is just a banality, but I am interested how could be it done.

例如,我正在文件夹及其子文件夹中搜索文件,并且我想打印到 cmd 当前文件夹 - 但我想在文件夹更改时重写它,而不仅仅是附加到末尾。我知道,这只是一种陈词滥调,但我很感兴趣如何做到这一点。

采纳答案by Neal Donnan

No, there is no command to do this

不,没有执行此操作的命令

回答by shoosh

Which one is that last line?
The one just above the prompt?
If that's what you mean then no, there is no way to clear just that using the prompt. If you're talking about doing it programatically, try using "\r"like so:

最后一行是哪一行?
提示上方的那个?
如果这就是你的意思,那么不,没有办法使用提示来清除它。如果您正在谈论以编程方式执行此操作,请尝试"\r"像这样使用:

print("\rNew text in the line");

回答by paxdiablo

If you're using the ANSI screen drivers, you can simply output "\r", your new line, then 'ESC', '[', 'K' which will clear to the end of the line.

如果您使用 ANSI 屏幕驱动程序,您可以简单地输出“\r”,您的新行,然后是 'ESC'、'['、'K',这将清除到该行的末尾。

But one thing to watch out for, you should limit your output length to the expected screen width so the lines don't wrap, such as

但是需要注意的一件事是,您应该将输出长度限制为预期的屏幕宽度,以便线条不会环绕,例如

c:\directory\really_big_name_too_long_..._so_Ive_put_some_ellipses_in.txt

In which case, you can probably just pad everything to 79 characters any way and it'll work with just the "\r" character, independent of ANSI driver.

在这种情况下,您可能可以以任何方式将所有内容填充为 79 个字符,并且它仅使用“\r”字符即可,独立于 ANSI 驱动程序。

回答by Keng

Are you looking for Ctl-c?

你在找 Ctl-c 吗?

回答by CleverEagle

You Could use brackets to record the previous, then not record the line you want to delete using the TYPE command: (This is just an example)

你可以用括号记录前面的,然后用TYPE命令不记录要删除的行:(这只是一个例子)

(
echo (Previous Outputs)
(This may happen as many times as needed)
) >> C:\file.txt
set /P thing=
cls
type C:\file.txt
pause