71

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?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Matt
  • 21,026
  • 18
  • 63
  • 115
  • Solution without 3rd party software here: https://stackoverflow.com/a/15747067/1683264 – rojo May 12 '17 at 12:14
  • I know I'm late to the party, but I think my answer (see below) is probably the closest to the intent of the question. – troyfolger May 27 '21 at 17:05

13 Answers13

55

If it would be acceptable to use PowerShell (and not cmd), then you can use Get-Clipboard exactly as you were looking for.

Get-Clipboard > myfile.txt

The advantage 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".

Dan Swain
  • 2,910
  • 1
  • 16
  • 36
Kpym
  • 3,743
  • 1
  • 21
  • 18
32

Clarifying an answer from @Kpym:

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

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

Adam Wise
  • 2,043
  • 20
  • 17
  • Since [DOSKEY macros](https://superuser.com/questions/1134368/create-permanent-doskey-in-windows-cmd) and batch scripts don't work reliably in a pipe, it seems like your only recourse for processing clipboard text in a proper pipeline (à la Unix, through programs like `sort`, `more`, `findstr` that read from stdin) is to use PowerShell. If you're a gentle soul who prefers CMD.EXE and doesn't thrive in PowerShell, you can construct the entire pipe _as_ a PowerShell command, like this: `powershell -command "get-clipboard | findstr STRING | sort | more"`. – TheDudeAbides Aug 28 '19 at 00:48
  • It seems that the redirection *outside* of PowerShell corrupts encoding (if the file is not pure ASCII). Redirecting *within* PowerShell helps. Though you have to specify the encoding in Windows PowerShell, as it defaults to UTF-16, what one hardly ever wants. In PowerShell Core, it's as easy as `pwsh -command "Get-Clipboard > file.txt"`, as PowerShell Core defaults to UTF-8. – Martin Prikryl Apr 23 '21 at 14:57
30

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.

Ted
  • 3,212
  • 25
  • 20
  • 13
    so there isn't anything that comes with windows? – Matt Jul 23 '13 at 20:51
  • From what was posted with this, it seems that dos does not have any such functionality and the clipboard can only be accessed with the help of higher level programming languages. – Ted Jul 23 '13 at 21:03
  • 6
    `clip.exe`, which I mention in the question, comes with Windows. – Matt Jul 24 '13 at 14:22
  • 2
    Don't expect miracles, an image or spreadsheet copied to a clipboard isn't going to look that good in a text file. Which is of course the reason why there isn't a standard command for this, too many accidents. – Hans Passant Jul 24 '13 at 16:04
  • 9
    @HansPassant Oh, I'm angry. It is not a reason! How many kittens died because of these accidents? – kan Feb 19 '16 at 16:58
  • You can also check [this GitHub repo](https://github.com/kpym/windows-paste) that contains [compiled binaries](https://github.com/kpym/windows-paste/releases) `ctrl-v.exe`, `ctrl-x.exe` and `ctrl-c.exe`. The `ctrl-v` is (almost) the same as the mentioned `paste`. – Kpym Dec 16 '18 at 16:59
  • 1
    This seems a bit too simplified, given the fact the clipboard can contain loads of stuff in parallel. Just copying a file in File Explorer in Win10 gives me 7 different byte stream blocks in the clipboard. None of which are the actual file, btw. – Nyerguds Apr 15 '19 at 11:40
  • `'paste' is not recognized as an internal or external command, operable program or batch file.` – Zimba Mar 28 '23 at 23:58
13

To get contents of clipboard

From win cmd:

powershell get-clipboard

or (via a temp file from HTML parser) on cmd:

echo x = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") > temp.vbs
echo WScript.Echo x >> temp.vbs
cscript //nologo temp.vbs

Output may be redirected to file.

Zimba
  • 2,854
  • 18
  • 26
6

Using the doskey macro definition feature, you can do:

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

Then (e.g.)

dir/b | clip
unclip | sort/r
  • 4
    Nice for usage on the command line, but Doskey macros don't work in batch files. (no critique, just to note) – Stephan Feb 01 '20 at 10:04
5

Pasteboard is another option. It can also work from WSL. First, install via choco:

choco install pasteboard

then the command is simply

pbpaste.exe > file.txt

And that works from cmd and wsl bash.

Adam Wise
  • 2,043
  • 20
  • 17
5

Well, from a million years ago, we did something like this:

type con > filename.txt

... and then you perform your paste operation (Ctrl-v, middle-click the mouse, or choose Edit->Paste from them menu) into the waiting prompt. This will capture the stdin buffer (the console device, named 'con'), and when an end-of-file is received, it will write the contents to the file. So, after your paste, you type 'Ctrl-z' to generate an EOF, and the type command terminates, and the contents of your paste buffer (the clipboard) are captured in 'filename.txt'.

troyfolger
  • 2,388
  • 2
  • 15
  • 14
  • 2
    I see. That's similar to `cat > filename.txt` (then paste and Ctrl+D) on *nix. It's been so long that can't remember if that would have worked in my original use case, but useful to know. Thanks. – Matt Aug 26 '21 at 17:17
4

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.
phuclv
  • 37,963
  • 15
  • 156
  • 475
Chris Thornton
  • 15,620
  • 5
  • 37
  • 62
2

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)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

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

Kevin Fegan
  • 1,292
  • 1
  • 16
  • 29
0

It may be possible with vbs:

Option Explicit
 
' Gets clipboard's contents as pure text and saves it or open it

Dim filePath : filePath = "clipboard.txt"

' Use the HTML parser to have access to the clipboard and get text content
Dim text : text = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")

' to open
If Not IsNull(text) then
Dim WshShell, somestring, txFldr2Open
Set WshShell = WScript.CreateObject("WScript.Shell")

txFldr2Open = "C:\Users"
txFldr2Open = text

somestring = "EXPLORER.exe /e," & txFldr2Open ', /select
WshShell.run somestring
Set WshShell = Nothing
else 
msgbox("Empty")

end if



' Create the file and write on it
msgbox(text)
Dim fileObj : Set fileObj = CreateObject("Scripting.FileSystemObject").CreateTextFile(filePath)
fileObj.Write(text)
fileObj.Close
Hakan
  • 240
  • 3
  • 4
0

You can use cbecho, a program I wrote in plain C. It will send any clipboard text to stdout, from where you can pipe it to other programs.

  • 1
    Please do not post link only answers, you need to at least use crucial parts of the link in the answer. Link's can change leaving the answer useless. – Gerhard May 10 '21 at 17:39
-10

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

notepad.exe file.txt

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

dwurf
  • 12,393
  • 6
  • 30
  • 42