Windows utility/tool command line to move clipboard to file

3

1

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

I have seen references to "paste" but this doesnt appear to be part of windows. I need to find a solution that works with standard w7 w8 w10 environments.

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

EDIT 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);
    }
}

phil soady

Posted 2016-03-06T13:09:18.043

Reputation: 225

Answers

2

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

JosefZ

Posted 2016-03-06T13:09:18.043

Reputation: 9 121

looks useful, was just testing c# code if (!Clipboard.ContainsImage()) return; Image img = Clipboard.GetImage(); but very useful to know i could start a powershell to access System.Windows.Forms.Clipboard]::GetImage() I test this soon – phil soady – 2016-03-06T17:03:09.663

The script code (the portion following -Command) seems to need to be quoted for this to run under Bash on Ubuntu on Windows. – Kenny Evitt – 2017-07-25T14:06:45.250

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 – 2018-09-04T14:38:19.090

3

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.

gilly3

Posted 2016-03-06T13:09:18.043

Reputation: 606

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?

DavidPostill

Posted 2016-03-06T13:09:18.043

Reputation: 118 938

I was looking for the image/binary data solution. Someone may find the script useful though. thanks David – phil soady – 2016-03-08T22:03:46.297

0

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.

KorkOoO

Posted 2016-03-06T13:09:18.043

Reputation: 75

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

Zimba

Posted 2016-03-06T13:09:18.043

Reputation: 107