Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy/cut a file (not the contents) to the clipboard in Windows on the command line?

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.

like image 930
rostok Avatar asked Jun 19 '13 10:06

rostok


People also ask

How do I copy from clipboard to file?

Open the file that you want to copy items from. Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

How do I copy from the Command Prompt?

The upgraded command prompt in Windows 10 is much simpler. You can copy and paste with the familiar CTRL + C to copy and CTRL + V to paste keyboard shortcuts.

How do I paste from clipboard to folder?

Replies (5)  If you've put an image in the clipboard by using Ctrl+c or by right-clicking and choosing Copy or Cut, then you paste that image by using Ctrl+v or Paste in whatever location you want the copy to go.


1 Answers

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.

like image 86
rostok Avatar answered Oct 10 '22 00:10

rostok