【问题标题】:How to copy/cut a file (not the contents) to the clipboard in Windows on the command line?如何在命令行上将文件(不是内容)复制/剪切到 Windows 中的剪贴板?
【发布时间】:2013-06-15 20:32:39
【问题描述】:

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

尤其是批处理脚本。我知道如何将内容复制到剪贴板 (type file | clip),但事实并非如此。我想拥有整个文件,就像在 Windows 资源管理器中按 Ctrl + C 一样。

【问题讨论】:

标签: windows shell command-line batch-file clipboard


【解决方案1】:

好吧,似乎最简单的方法是创建一个小型 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);
        }
    }
}

2017 年编辑:这是一个 github repo,包含源代码和二进制文件。

【讨论】:

  • 谢谢,这太棒了,我一直在寻找这样的东西很长一段时间。顺便说一句,我花了两天时间弄清楚我是否可以在 vb 中做到这一点,然后才意识到我已经在 c:\Windows\Microsoft.NET 下方的某个地方有一个 C# 编译器@
  • 如果其他答案都没有回答你的问题,你应该接受这个答案,即使是你自己的,这样我们其他人可以更容易地找到答案。
  • 很好的解决方案。对于我的使用,我将此实用程序作为fileclip.exe 添加到系统路径中。从路径中使用它,我必须将 System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) 更改为 System.IO.Directory.GetCurrentDirectory() 才能使用相对路径。
  • 一些 powershell 等价物:github.com/…
【解决方案2】:

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

type \path\to\file|clip

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

【讨论】:

    【解决方案3】:

    我一直希望在 Emacs 中使用它,因此,受这个问题、答案 here 和大量 NIH 综合症的启发,我编写了一个 C 版本,可在

    https://github.com/roryyorke/picellif

    picellif 还处理通配符(我不清楚 rostok 的 C# 版本是否支持)。

    【讨论】:

    • 我也希望它用于 Emacs 和其他命令行用途 :) 我一直在使用上面 rostok 答案中的 C# 代码,但如果你有 Windows 二进制文件,我很乐意使用它,只是因为你用 Emacs 写的。
    • @MiserableVariable 在 github 上有 Windows 二进制文件(查看发布选项卡下)。过去三周我一直在使用 0.2.0,但没有发现任何错误。
    • 哎呀不知道我是怎么错过的。现在你多了一个用户 :)
    • 嗨 Roy 只是想让你知道这是非常有用的,我使用的是同名 picellif,希望有一天能记住它的名字,而不必总是通过 ls /usr/local/bin : )
    • @MiserableVariable 是的,不幸的是,“fileclip”这个名字已经被使用了。 Emacs 25 将具有可加载模块(默认情况下禁用),我想将其重写为这样的模块; Windows 上的进程启动时间会使 picellif 在第一次使用时变得非常缓慢。
    【解决方案4】:

    copymove 是(一些)分别复制/粘贴和剪切/粘贴文件的批处理命令。我们在处理文件时不使用术语粘贴或剪切,但如果我理解您,需要将文件复制到另一个位置并将文件移动到另一个位置。

    【讨论】:

    • 不,这与文件复制无关(复制、xcopy、移动等非常基本)。问题是关于如何将文件放入系统剪贴板。
    • 好的。我想如果我们了解文件的类型以及它是如何使用的,那么有人可能会提供帮助。
    【解决方案5】:

    你可以试试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.
    

    示例:将反斜杠转换为正斜杠。假设您在Notepad 中打开了以下文本:

    foo/bar/systems/alpha1.cpp
    foo/bar/systems/alpha2.cpp
    foo/bar/systems/beta1.cpp
    

    由于某种原因,您需要采用如下格式的第一行:

    foo\bar\systems\alpha1.cpp
    

    那么你可以这样做:

    1. 使用 SHIFT + CURSOR 键标记第一行。
    2. Ctrl + CCtrl + Insert 复制到剪贴板
    3. 在 Windows 命令行上,运行以下命令(例如,从批处理文件):

      sfk fromclip +filter -rep x/x\x +toclip
      
    4. 返回编辑器,按 Ctrl + VShift + Insert,从剪贴板粘贴结果。

    如您所见,该行更改为“foo\bar\systems\alpha1.cpp”。

    【讨论】:

      【解决方案6】:
      【解决方案7】:

      我也遇到了类似的问题,为此我写了一个ahk脚本
      (需要Gdip库编译)

      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
      

      编译后,我们可以使用CopyToClip.exe &lt;your_file_path&gt;将文件内容复制到剪贴板,但目前只支持文本和图片。

      【讨论】:

        猜你喜欢
        • 2010-11-22
        • 2020-05-15
        • 2017-02-21
        • 2020-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多