如何在命令行中将文件(而不是内容)复制/剪切到Windows中的剪贴板?

ros*_*tok 10 windows shell clipboard command-line batch-file

有没有办法从命令行将文件复制(或剪切)到Windows剪贴板?

特别是批处理脚本.我知道如何将内容复制到剪贴板(type file | clip),但事实并非如此.我希望拥有整个文件,因为我会在Windows资源管理器中按Ctrl+ C.

ros*_*tok 11

好吧,似乎最简单的方法是创建一个小型C#工具,它接受参数并将它们存储在剪贴板中:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2017编辑:这是一个包含源代码和二进制文件的github仓库.

  • 如果其他答案都没有回答你的问题,你应该接受这个答案,即使它是你自己的,这样我们其他人也可以更容易地找到答案。 (2认同)

moj*_*ojo 5

这会将文件的内容放入剪贴板(由 clip.exe 完成)。

type \path\to\file|clip
Run Code Online (Sandbox Code Playgroud)

要获得实际文件,您可能不得不求助于其他一些编程语言,如 VBScript 或 PowerShell 来访问 Windows API。当您 CTRL+C 一个文件时,我不完全确定 Explorer 将什么放入剪贴板。我怀疑它使用通知系统来做一些比将文件路径放在那里更智能的事情。根据 CTRL+V 的上下文,你会得到一些东西(资源管理器、Word)或什么都没有(记事本)。