Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...