CMD:将所有屏幕内容导出到文本文件 [英] CMD: Export all the screen content to a text file

查看:1086
本文介绍了CMD:将所有屏幕内容导出到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在命令提示符下-如何将屏幕上的所有内容导出到文本文件(基本上是复制命令,只是不使用右键单击和剪贴板)

In command prompt - How do I export all the content of the screen to a text file(basically a copy command, just not by using right-clicking and the clipboard)

此命令有效,但仅对您执行的命令有效,也不适用于

This command works, but only for the commands you executed, not the actual output as well

doskey /HISTORY > history.txt


推荐答案

如果要附加文件要不断制作新内容/删除旧内容,请使用双> 标记。单个> 标记将覆盖文件的所有内容。

If you want to append a file instead of constantly making a new one/deleting the old one's content, use double > marks. A single > mark will overwrite all the file's content.

覆盖文件

MyCommand.exe>file.txt

^这将打开file.txt(如果已存在)并覆盖数据,或创建一个新文件并用您的输出填充

^This will open file.txt if it already exists and overwrite the data, or create a new file and fill it with your output

从端点添加文件

MyCommand.exe>>file.txt

^如果文件已经存在,它将从文件的当前结尾追加file.txt,或创建一个新文件并用您的输出填充它。

^This will append file.txt from its current end of file if it already exists, or create a new file and fill it with your output.

我的批次-fu随着时间的推移而有所改进,因此这里有一些小的更新。

My batch-fu has improved over time, so here's some minor updates.

如果要区分正确使用标准流 STDOUT / STDERR ,您可以使用语法上的微小变化。对于这些示例,我将只使用> 进行覆盖,但是对于>> 来说,它们可以很好地工作。

If you want to differentiate between error output and normal output for a program that correctly uses Standard streams, STDOUT/STDERR, you can do this with minor changes to the syntax. I'll just use > for overwriting for these examples, but they work perfectly fine with >> for append, in regards to file-piping output re-direction.

1 >>> 或> 是STDOUT的标志。如果您实际上需要在重定向符号之前输出数字一或二,这可能会导致奇怪的,不直观的错误(如果您不知道这种机制)。在将单个结果编号输出到文件时,这一点尤其重要。 2 重定向符号用于STDERR。

The 1 before the >> or > is the flag for STDOUT. If you need to actually output the number one or two before the re-direction symbols, this can lead to strange, unintuitive errors if you don't know about this mechanism. That's especially relevant when outputting a single result number into a file. 2 before the re-direction symbols is for STDERR.

现在您知道自己有多个流可用,这是一个展示输出为 nul 的好处的好时机。现在,输出到 nul 在概念上与输出到文件的方式相同。您在控制台中看不到内容。

Now that you know that you have more than one stream available, this is a good time to show the benefits of outputting to nul. Now, outputting to nul works the same way conceptually as outputting to a file. You don't see the content in your console. Instead of it going to file or your console output, it goes into the void.

STDERR 归档并抑制 STDOUT

STDERR to file and suppress STDOUT

MyCommand.exe 1> nul 2> errors.txt

STDERR 归档以仅记录错误。将STDOUT保留在控制台中

STDERR to file to only log errors. Will keep STDOUT in console

MyCommand.exe 2> errors.txt

STDOUT 归档并抑制 STDERR

STDOUT to file and suppress STDERR

MyCommand.exe 1> file.txt 2> nul

STDOUT 归档。将STDERR保留在控制台中

STDOUT only to file. Will keep STDERR in console

MyCommand.exe 1> file.txt

STDOUT 到一个文件 STDERR 另一个文件

MyCommand.exe 1> stdout.txt 2> errors.txt

我在这里唯一需要说明的是,如果其中一个流从未被使用过,它可以为未使用的流创建一个0字节的文件。基本上,如果没有发生错误,则最终可能会得到一个0字节的errors.txt文件。

The only caveat I have here is that it can create a 0-byte file for an unused stream if one of the streams never gets used. Basically, if no errors occurred, you might end up with a 0-byte errors.txt file.

在编写直接写入 STDERR 的控制台应用程序时,我开始注意到奇怪的行为,并意识到如果我希望使用basic命令时将错误输出转至同一文件,管道,我要么必须合并流 1 2 ,要么只使用 STDOUT 。这个问题的问题是我不知道合并流的正确方法,这是

I started noticing weird behavior when writing console apps that wrote directly to STDERR, and realized that if I wanted my error output to go to the same file when using basic piping, I either had to combine streams 1 and 2 or just use STDOUT. The problem with that problem is I didn't know about the correct way to combine streams, which is this:

%command%>输出文件2>& 1

因此,如果要全部 STDOUT STDERR 通过管道传输到相同的流中,请确保像这样使用:

Therefore, if you want all STDOUT and STDERR piped into the same stream, make sure to use that like so:

MyCommand.exe> file.txt 2>& 1

重定向器实际上默认为 1> 1>> ,即使您未在其前面明确使用 1 在其前面使用数字,然后 2>& 1 合并流。

The redirector actually defaults to 1> or 1>>, even if you don't explicitly use 1 in front of it if you don't use a number in front of it, and the 2>&1 combines the streams.

如果要完全抑制STDOUT和STDERR,可以使用这种方法。作为警告,并不是所有的文本管道都使用STDOUT和STDERR,但它适用于绝大多数用例。

If you want to completely suppress STDOUT and STDERR you can do it this way. As a warning not all text pipes use STDOUT and STDERR but it will work for a vast majority of use cases.

STD *为空
MyCommand.exe> nul 2>& 1

如果您想要的只是您从CMD或Powershell会话获得的命令输出刚结束,或任何其他外壳,通常您可以从该会话中选择该控制台,先 CTRL + A 选择所有内容,然后选择 CTRL + C 复制内容。然后,您可以对剪贴板中复制的内容进行任何操作。

If all you want is the command output from a CMD or Powershell session that you just finished up, or any other shell for that matter you can usually just select that console from that session, CTRL + A to select all content, then CTRL + C to copy the content. Then you can do whatever you like with the copied content while it's in your clipboard.

这篇关于CMD:将所有屏幕内容导出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆