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

三世轮回 提交于 2019-12-18 11:07:01

问题


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

clip < file.txt

(Now the contents of file.txt is in the clipboard.)

How can I do the opposite:

???? > file.txt

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


回答1:


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

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

or

paste > filename

to paste the clipboard contents to the specified file.




回答2:


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

Get-Clipboard > myfile.txt

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

Note: In place of clip you can use Set-Clipboard that has more options.

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




回答3:


Clarifying an answer from @Kpym:

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

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




回答4:


Using the doskey macro definition feature, you can do:

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

Then (e.g.)

dir/b | clip
unclip | sort/r



回答5:


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. (dave@basicguru.com)



回答6:


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

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 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

DELPHI SOURCE INCLUDED!

And hey, here it is (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.



回答7:


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

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

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




回答8:


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




回答9:


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

notepad.exe file.txt

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



来源:https://stackoverflow.com/questions/17819814/how-can-you-get-the-clipboard-contents-with-a-windows-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!