有没有办法从命令行复制(或剪切)到Windows剪贴板的文件?
特别是使用批处理脚本。 我知道如何将内容复制到剪贴板( type file | clip
),但事实并非如此。 我想拥有整个文件,因为我会在Windows资源pipe理器中按Ctrl + C。
好的,看起来最简单的方法是创建一个小的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回购与源和二进制。
这将把文件的内容放入剪贴板(由clip.exe完成)。
type \path\to\file|clip
要获得实际的文件,你可能不得不求助于其他编程语言,如VBScript或PowerShell来访问Windows API。 我不完全确定当您按Ctrl + C文件时,资源管理器放入剪贴板的内容。 我怀疑它使用通知系统来做一些比放置文件路径更智能的东西。 根据CTRL + V的上下文,你会得到一些东西(资源管理器,Word)或没有(记事本)。
copy
和move
是(一些)分别复制/粘贴和剪切/粘贴文件的批处理命令。 在处理文件时,我们不使用术语“粘贴”或“剪切”,但如果我理解您需要将文件复制到另一个位置并将文件移动到另一个位置。
你可以尝试瑞士文件刀 (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.
示例:将反斜杠变成正斜杠。 假设你在记事本中打开了以下文本:
foo/bar/systems/alpha1.cpp foo/bar/systems/alpha2.cpp foo/bar/systems/beta1.cpp
出于某种原因,你需要像这样的格式的第一行:
foo\bar\systems\alpha1.cpp
那么你可以这样做:
在Windows命令行上,运行此命令(例如,从批处理文件):
sfk fromclip +filter -rep x/x\x +toclip
回到编辑器,按下Ctrl + V或Shift + Insert,粘贴剪贴板中的结果。
如您所见,该行更改为“foo \ bar \ systems \ alpha1.cpp”。
我一直希望这个在Emacs中使用,所以,受到这个问题的启发,这里有一个答案,还有一个很好的NIH综合征,我写了一个C版本,
https://github.com/roryyorke/picellif
picellif也处理通配符(我不清楚rostok的C#版本是否)。
您可以使用命令行文件复制和粘贴工具: http : //sbytestream.net/Software/Detail/2