12
votes

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.

8
@cb0 please look at the comment to the thread you linked.Endoro

8 Answers

12
votes

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.

5
votes

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

3
votes

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

2
votes

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.

2
votes

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

0
votes

I also encountered a similar problem, for which I wrote an ahk script
(requires Gdip library to compile)

CopyToClip.ahk:

fileName := %0%

if( !FileExist(fileName) )
{
    MsgBox, %fileName% not found!
    return
}

;MsgBox, %fileName%
file := FileOpen(fileName, "r")
if( ErrorLevel )
{
    MsgBox, Open %fileName% error!
    return
}
encoding := File.Encoding
fileSize := File.Length


; Read as text?
readAs := "unkonw"
SplitPath, fileName, name, dir, ext, name_no_ext, drive
if( ext == "txt" || ext == "md" || ext == "bat" || ext == "cmd" || ext == "ahk" || ext == "ini" || ext == "sfo")
{
    readAs := "text"
}
else if( ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "xbt" || ext == "img" )
{
    readAs := "image"
}
else if( fileSize > 10240 )
{
    MsgBox, Unsupport file size %fileSize%
    return
}
else
{
    ToolTip, Unsupport file type %ext%`, but read as text.
    readAs := "text"
}
;MsgBox, %readAs%

if( readAs == "text" )
{
    fileContent := File.Read()
    Clipboard := fileContent
}
else if( readAs == "image" )
{
    pToken := Gdip_Startup()
    Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromFile(fileName))
    Gdip_DisposeImage(pBitmap)
    Gdip_Shutdown(pToken)
    ;file.RawRead(fileContent, fileSize)
}else
{
    MsgBox, Unkonw Error!
    return
}

if( ErrorLevel )
{
    MsgBox, Read %fileName% error!
    return
}

ToolTip, %fileName%`nSize: %fileSize%
Sleep, 400
ExitApp

After compiling, we can use CopyToClip.exe <your_file_path> to copy the contents of the file to the clipboard, but currently only supports text and image.

-1
votes

Windows Powershell has almost the same cp as Linux

You should check out Windows Powershell, you can use cp command for it.

Simple Command Line Copying in Windows

  1. Open Powershell by typing powershell in cmd.
  2. Then do this cp path_to_file/folder_to_copy path_to_destination

Some Examples

Copying a File into the Directory you are in!

cp path_to_file .

Here the . means the current directory.

Copying to a Protected Directory

If you would like to copy a file into a restricted folder, you would need administrator privileges