8

I have seen a similar tool CLIP, but it works into clipboard not the other direction.

I have seen references to "paste" but this doesn't appear to be part of Windows. I need to find a solution that works with standard Windows 7, 8 and 10 environments.

Clip is distributed. Does anybody a tool that works from clipboard to file?

Seems it might be possible to do with a PowerShell command.

This C# program I just wrote does what I would to do with a PowerShell:

class Clip2File
{
    [STAThread()]
    static void Main(string[] args)
    {
        var fileName = @"c:\demo.png";
        if (args.Length != 0){
            fileName = args[0];
        }
        if (!Clipboard.ContainsImage()) return;
        Image img = Clipboard.GetImage();
        img?.Save(fileName, ImageFormat.Png);
    }
}
0

5 Answers 5

5

Windows cmd (if clipboard contains text data):

Powershell -command Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Clipboard]::GetText()

Example shows that above command adds two bytes to output (Cr and Lf):

==> >1049363a.txt echo first line

==> >>1049363a.txt echo 2nd line

==> clip<1049363a.txt

==> >1049363b.txt Powershell -command Add-Type -AssemblyName System.Windows.Forms;[System.W
indows.Forms.Clipboard]::GetText()

==> findstr /N "^" 1049363*.txt
1049363a.txt:1:first line
1049363a.txt:2:2nd line
1049363b.txt:1:first line
1049363b.txt:2:2nd line
1049363b.txt:3:

==> dir 1049363*.txt |find ".txt"
06.03.2016  17:24                22 1049363a.txt
06.03.2016  17:24                24 1049363b.txt

==>

Read more about .NET Framework Clipboard Class methods.

Edit answers extended topic (save image, cf. ImageFormat Class):

Add-Type -AssemblyName System.Windows.Forms  ### not necessary in PowerShell_ISE
if ($([System.Windows.Forms.Clipboard]::ContainsImage())) {
    $image = [System.Windows.Forms.Clipboard]::GetImage()
    $filename='d:\test\test.png'             ### edit to fit in your circumstances
    
    [System.Drawing.Bitmap]$image.Save($filename,
                    [System.Drawing.Imaging.ImageFormat]::Png)
    
    Write-Output "clipboard content saved as $filename"
} else {
    Write-Output "clipboard does not contains image data"
}
2
  • The script code (the portion following -Command) seems to need to be quoted for this to run under Bash on Ubuntu on Windows. Commented Jul 25, 2017 at 14:06
  • the Clipboard class is available since .NET 1.1 but somehow when I tried Powershell -version 2 -command Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Clipboard]::GetText() it doesn't run. It only works with version 3 and above
    – phuclv
    Commented Sep 4, 2018 at 14:38
7

You can use the PowerShell cmdlet Get-Clipboard (aliased as gcb).

Write clipboard text to a file:

gcb > myfile.txt

It does have an option for getting an image from the clipboard, -Format Image, but that seems to only provide meta data, so it may not be helpful in writing an image to a file.

1
  • To run from cmd.exe: Powershell.exe -command Get-Clipboard
    – idbrii
    Commented Sep 20, 2023 at 23:23
2

Does anybody have tool that works from clipboard to file?

This can done using a batch file containing some embedded VBS.

GetClip.cmd:

@echo off
(
echo set objHTML = CreateObject("htmlfile"^)
echo ClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text"^)
echo set objFSO = CreateObject("Scripting.FileSystemObject"^)   
echo set objFile = objFSO.OpenTextFile("clip.txt", 2, true^)
echo objFile.WriteLine ClipboardText
echo objFile.Close
) > "%temp%\clip.vbs"
"%temp%\clip.vbs"
endlocal

Notes:

  • Clipboard contents are written to clip.txt in the current directory

Example Usage:

F:\test>type LoremIpsum.txt
Id pri magna tibique, vel et eruditi perpetua, numquam mandamus sed et.
Te nam diam veritus.
Ad est quaestio ocurreret, at vix modo prima officiis.
Modus principes definiebas mei et, atqui exerci ea sit.
An eirmod saperet dissentiunt sea, esse postea eleifend ex eam.

F:\test>clip<LoremIpsum.txt

F:\test>getclip

F:\test>type clip.txt
Id pri magna tibique, vel et eruditi perpetua, numquam mandamus sed et.
Te nam diam veritus.
Ad est quaestio ocurreret, at vix modo prima officiis.
Modus principes definiebas mei et, atqui exerci ea sit.
An eirmod saperet dissentiunt sea, esse postea eleifend ex eam.


F:\test>

Source: Based on Batch code to paste from clipboard to a file?

1
  • I was looking for the image/binary data solution. Someone may find the script useful though. thanks David
    – phil soady
    Commented Mar 8, 2016 at 22:03
1

Best way I know, is by using a standalone tool called WINCLIP .

You can get it from here: Outwit

Usage:

  • Save clipboard to file: winclip -p file.txt

  • Copy stdout to clipboard: winclip -c Ex: Sed 's/find/replace/' file | winclip -c

  • Pipe clipboard to sed: winclip -p | Sed 's/find/replace/'

  • Use winclip output (clipboard) as an argument of another command:

    FOR /F "tokens=* usebackq" %%G in ('winclip -p') Do (YOUR_Command %%G )

You might also want to take a look at getclip & putclip tools: CygUtils for Windows but winclip is better in my opinion.

0

If you have AutoHotKey:

FileAppend %ClipboardAll%, FileName.raw, UTF-8

will save clipboard to file as raw data. See discussion at: https://stackoverflow.com/questions/45648721/autohotkey-how-to-take-a-screenshot-and-to-paste-it-to-a-jpg-file/57142781?noredirect=1#comment101068737_57142781

A custom paste.exe is available (similar to clip.exe in Win 10) here: https://www.c3scripts.com/tutorials/msdos/paste.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .