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

大憨熊 提交于 2019-11-29 09:46:41

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.

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

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

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.

Endoro

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

You can use Command Line File Copy and Paste utilities available here : http://sbytestream.net/Software/Detail/2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!