17

Is there a way to copy (or cut) a file to the Windows clipboard from the command line?

In particular with a batch script. I know how to copy the contents to the clipboard (type file | clip), but this is not the case. I want to have the whole file as I would press Ctrl + C in Windows Explorer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rostok
  • 2,057
  • 2
  • 18
  • 20
  • Have a look here http://serverfault.com/questions/162273/how-to-send-a-file-into-clipboard-from-command-line-cli – cb0 Jun 19 '13 at 10:53
  • 1
    @cb0 please look at the [comment](http://serverfault.com/a/162275) to the thread you linked. – Endoro Jun 19 '13 at 11:17
  • possible duplicate of [Is there a Windows API to programmatically Cut / Copy / Paste files via Explorer?](http://stackoverflow.com/questions/9845056/is-there-a-windows-api-to-programmatically-cut-copy-paste-files-via-explorer) – Synetech Dec 09 '13 at 01:18

6 Answers6

15

OK, it seems the easiest way was to create a small C# tool that takes arguments and stores them in the clipboard:

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace File2Clip
{
    public class App
    {
        [STAThread]
        static void Main(string[] args)
        {
            List<string> list = new List<string>();

            string line;
            while(!string.IsNullOrEmpty(line = Console.ReadLine())) list.Add(line);
            foreach (string s in args) list.Add(s);

            StringCollection paths = new StringCollection();
            foreach (string s in list) {
            Console.Write(s);
                paths.Add( 
                    System.IO.Path.IsPathRooted(s) ? 
                      s : 
                      System.IO.Directory.GetCurrentDirectory() + 
                        @"\" + s);
            }
            Clipboard.SetFileDropList(paths);
        }
    }
}

2017 edit: Here's a github repo with both source and binary.

rostok
  • 2,057
  • 2
  • 18
  • 20
  • 1
    Thank you, this is awesome and I had been looking for something like this for a long time. BTW I spent two days figuring out if I can do this in vb before realized I already had a `C#` compiler somewhere below `c:\Windows\Microsoft.NET` – Miserable Variable Nov 01 '13 at 23:34
  • 2
    If none of the other answers answer your question, you should accept this answer, even if it is your own, so that we others can find the answer more easily. – Museful Dec 01 '13 at 12:19
  • 1
    Good solution. For my usage, I added this utility as `fileclip.exe` to the system path. Using it from the path, I had to change `System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])` to `System.IO.Directory.GetCurrentDirectory()` for relative paths to work. – John McCarthy Dec 11 '13 at 20:33
  • Some powershell equivalents : https://github.com/search?l=PowerShell&q=Clipboard.SetFileDropList&type=Code – Кое Кто Oct 25 '20 at 19:51
7

This would place the contents of the file into the clipboard (accomplished by clip.exe).

type \path\to\file|clip

To get the actual file, you'll probably have to resort to some other programming language, like VBScript or PowerShell to access Windows API's. I'm not entirely certain what Explorer puts into the clipboard when you CTRL+C a file. I suspect it uses the notification system to do something more intelligent than put the path to the file there. Depending on the context of the CTRL+V, you'll get something (Explorer, Word) or nothing (Notepad).

mojo
  • 4,050
  • 17
  • 24
3

I've forever wanted this to use in Emacs, so, inspired by this question, an answer here, and a goodly amount of NIH syndrome, I've written a C version available at

https://github.com/roryyorke/picellif

picellif also handles wildcards (it's not clear to me if rostok's C# version does or not).

Rory Yorke
  • 2,166
  • 13
  • 13
  • I want it for Emacs and other command line uses too :) I have been using the C# code from rostok's answer above but if you have windows binaries I would by happy to use that instead, just because you wrote it Emacs use. – Miserable Variable Aug 04 '15 at 01:56
  • @MiserableVariable There are windows binaries at github (look under the releases tab). I've been using 0.2.0 for the past three weeks without noticing bugs. – Rory Yorke Aug 04 '15 at 18:37
  • Oops don't know how I missed it. Now you have one more user :) – Miserable Variable Aug 05 '15 at 01:19
  • Hi Roy Just wanted to let you know this is very useful and I am using the same name picellif and some day hope to remember its name without always having to go through ls /usr/local/bin :) – Miserable Variable May 02 '16 at 03:18
  • @MiserableVariable yeah, unfortunately the name "fileclip" was already in use. Emacs 25 will have loadable modules (disabled by default, though), and I'd like to rewrite this as such a module; the process startup time on Windows can make picellif annoyingly slow on first use. – Rory Yorke May 03 '16 at 18:15
  • In Cygwin, use `winpty picellif your_file_to_copy.exe` – John Chain Jan 16 '19 at 16:18
2

You can try Swiss File Knife (SFK):


sfk toclip
 Copy stdin to clipboard as plain text.

    type test.txt | sfk toclip
       Copies the content of ASCII file test.txt into the clipboard.

    sfk list | sfk toclip
       Copies a file listing of the current dir into the clipboard.

sfk fromclip [-wait] [-clear]

 Dump plain text content from the clipboard to the terminal.

   -wait : block until plain text is available.
   -clear: empty the clipboard after reading it.

Example: turn backslashes into forward slashes. Imagine you have the following text open within Notepad:

foo/bar/systems/alpha1.cpp
foo/bar/systems/alpha2.cpp
foo/bar/systems/beta1.cpp

And for some reason you need the first line in a format like this:

foo\bar\systems\alpha1.cpp

Then you may do it this way:

  1. Mark the first line using SHIFT + CURSOR keys.
  2. Press Ctrl + C or Ctrl + Insert to copy it into clipboard
  3. On the Windows command line, run this command (for example, from a batch file):

    sfk fromclip +filter -rep x/x\x +toclip
    
  4. Back in the editor, press Ctrl + V or Shift + Insert, pasting the result from the clipboard.

As you see, the line changed into "foo\bar\systems\alpha1.cpp".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Endoro
  • 37,015
  • 8
  • 50
  • 63
2

copy and move are (some of) the batch commands that copy/paste and cut/paste files, respectively. We don't use the terms paste or cut when dealing with files but if I understand you there is a need to copy a file to another location and to move files to another location.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • 1
    no, this is not about file copying (copy, xcopy, move etc are pretty basic). problem is about how to place file into system clipboard. – rostok Jun 19 '13 at 14:03
  • Ok. I guess if we understood the type of file and how it is being used someone might have an idea to help. – foxidrive Jun 19 '13 at 15:11
0

You can use Command Line Copy and Paste Files utilities on Sid's Bytestream.

bluish
  • 26,356
  • 27
  • 122
  • 180
Siddharth B
  • 344
  • 3
  • 11