如何使用 Windows 命令获取剪贴板内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17819814/
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-09 10:44:43  来源:igfitidea点击:

How can you get the clipboard contents with a Windows command?

windowscmdclipboardcopy-paste

提问by Matt

For example, I can copy a file to the clipboard like this:

例如,我可以像这样将文件复制到剪贴板:

clip < file.txt

(Now the contents of file.txtis in the clipboard.)

(现在 的内容file.txt在剪贴板中。)

How can I do the opposite:

我该如何做相反的事情:

???? > file.txt

So that the contents of the clipboard will be in file.txt?

这样剪贴板的内容将在file.txt

采纳答案by Ted

You can use the paste.exe software in order to paste text just like you are describing.

您可以使用 paste.exe 软件来粘贴您所描述的文本。

http://www.c3scripts.com/tutorials/msdos/paste.html

http://www.c3scripts.com/tutorials/msdos/paste.html

With it you can do:

有了它,您可以:

paste | command

to paste the contents of the windows clipboard into the input of the specified command prompt

将windows剪贴板的内容粘贴到指定命令提示符的输入中

or

或者

paste > filename

to paste the clipboard contents to the specified file.

将剪贴板内容粘贴到指定文件。

回答by Kpym

If you accept to use PowerShell(and not cmd) the you can use Get-Clipboardexactly as you was looking for.

如果您接受使用PowerShell(而不是cmd),您可以完全按照您的要求使用Get-Clipboard

Get-Clipboard > myfile.txt

The adventage of this method is that you have nothing to install.

这种方法的优点是您无需安装任何东西。

Note:In place of clipyou can use Set-Clipboardthat has more options.

注意:clip您可以使用具有更多选项的Set-Clipboard代替。

Note 2:If you really want to run it from cmd, you can call powershellas in the following example powershell -command "Get-Clipboard | sort | Set-Clipboard".

注意 2:如果你真的想从 运行它cmd,你可以powershell在下面的例子中调用powershell -command "Get-Clipboard | sort | Set-Clipboard"

回答by Adam Wise

Clarifying an answer from @Kpym:

澄清@Kpym 的回答:

powershell -command "Get-Clipboard" > file.txt

This directly answers the question without using a 3rd party tool.

这直接回答了问题,而无需使用 3rd 方工具。

回答by Thomas Knudsen

Using the doskey macro definition feature, you can do:

使用 doskey 宏定义功能,您可以执行以下操作:

doskey unclip=(powershell -command "Get-Clipboard") $*

Then (e.g.)

然后(例如)

dir/b | clip
unclip | sort/r

回答by foxidrive

There are third party clip commands that work bidirectionally.

有双向工作的第三方剪辑命令。

Here's one:

这是一个:

    CLIP - Copy the specified text file to the clip board
    Copyright (c) 1998,99 by Dave Navarro, Jr. ([email protected])

回答by Chris Thornton

I have a pair of utilities (from before the Clip command was part of windows) available on this page:

我在此页面上提供了一对实用程序(在 Clip 命令是 Windows 的一部分之前):

http://www.clipboardextender.com/general-clipboard-use/command-window-output-to-clipboard-in-vista

http://www.clipboardextender.com/general-clipboard-use/command-window-output-to-clipboard-in-vista

There are two utilities in there, Clip2DOS and DOS2Clip. You want Clip2DOS:

那里有两个实用程序,Clip2DOS 和 DOS2Clip。你想要 Clip2DOS:

Clip2DOS Copyright 2006 Thornsoft Development Dumps clipboard text (1024 bytes) to stdout.
Usage: Clip2Dos.exe > out.txt Result: text is in the file. Limits: 1024 bytes. License: Free, as in Free Beer! http://www.thornsoft.com/dist/techsupport/dos2clip.zip

Clip2DOS 版权所有 2006 Thornsoft Development 将剪贴板文本(1024 字节)转储到标准输出。
用法:Clip2Dos.exe > out.txt 结果:文本在文件中。限制:1024 字节。许可证:免费,就像免费啤酒一样! http://www.thornsoft.com/dist/techsupport/dos2clip.zip

DELPHI SOURCE INCLUDED!

包括德尔福源!

And hey, here it is (Clip2DOS.dpr) :

嘿,这是(Clip2DOS.dpr):

{Clip2DOS - copyright 2005 Thornsoft Development, Inc.  All rights reserved.}
program Clip2Dos;

{$APPTYPE CONSOLE}

uses
  Clipbrd,
  ExceptionLog,
  SysUtils;

var
   p : Array[0..1024] of Char;
begin
  try
    WriteLn('Clip2DOS Copyright 2006 Thornsoft Development');
    Clipboard.GetTextBuf(p,1024);
    WriteLn(p);
  except
    //Handle error condition
    on E: Exception do
            begin
              beep;
              Writeln(SysUtils.format('Clip2DOS - Error: %s',[E.Message]));
              ExitCode := 1;    //Set ExitCode <> 0 to flag error condition (by convention)
            end;
  end
end.

回答by Kevin Fegan

Here is the CLIP program by Dave Navarro, as referred to in the answer by @foxidrive. It is mentioned in an article here: copying-from-clipboard-to-xywrite

这是 Dave Navarro 的 CLIP 程序,如@foxidrive 的回答中所述。在这里的一篇文章中提到:copying-from-clipboard-to-xywrite

A link to the download, along with many other resources is on this page: http://www.lexitec.fi/xywrite/utility.html

下载链接以及许多其他资源位于此页面上:http: //www.lexitec.fi/xywrite/utility.html

Here is a direct link to the download: "DOWNLOAD Clip.exeCopy from and to the clipboard by Dave Navarro, Jr."

这是下载的直接链接:“DOWNLOAD Clip.exeCopy from and to the clipboard by Dave Navarro, Jr.”

回答by Carlos Castillo

I know I'm really late to answer this, but you could write:

我知道我回答这个问题真的晚了,但你可以写:

type file.txt | clip

So the content of the clipboard will be the content of file.txt

所以剪贴板的内容将是 file.txt 的内容

回答by Nephew of Stackoverflow

I am not sure if this command was not supported at that time or not, but it surely does work

我不确定当时是否不支持此命令,但它确实有效

clip > file.txt

回答by dwurf

This dirty trick worked for my needs, and it comes with Windows!

这个肮脏的把戏满足了我的需要,而且它是 Windows 自带的!

notepad.exe file.txt

Ctrl+ V, Ctrl+ S, Alt+ F, X

Ctrl+ V, Ctrl+ S, Alt+ F,X