有没有一种方法可以通过命令行将文件复制(或剪切)到Windows剪贴板中?
尤其是在批处理脚本中。我知道如何将内容复制到剪贴板(type file | clip
),但这并不是我想要的。我希望像在Windows资源管理器中按下Ctrl + C那样,整个文件都被复制到剪贴板中。
有没有一种方法可以通过命令行将文件复制(或剪切)到Windows剪贴板中?
尤其是在批处理脚本中。我知道如何将内容复制到剪贴板(type file | clip
),但这并不是我想要的。我希望像在Windows资源管理器中按下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资料库,包含源代码和二进制文件。
c:\Windows\Microsoft.NET
下有一个 C#
编译器之前,我花了两天时间弄清楚是否可以在 vb 中完成这个任务。 - Miserable VariableSystem.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])
更改为System.IO.Directory.GetCurrentDirectory()
以使相对路径正常工作。 - John McCarthytype \path\to\file|clip
我一直希望在Emacs中使用这个功能,因此受到这个问题的启发,找到了一个答案(这里),并且写了一个C语言版本,可在以下网址查看:
https://github.com/roryyorke/picellif
picellif还可以处理通配符(对我来说不清楚rostok的C#版本是否可以)。
winpty picellif your_file_to_copy.exe
。 - John ChainYou 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.
例子:将反斜杠转换为正斜杠。假设您在记事本中打开以下文本:
foo/bar/systems/alpha1.cpp
foo/bar/systems/alpha2.cpp
foo/bar/systems/beta1.cpp
如果出于某种原因,您需要将第一行格式化为以下格式:
foo\bar\systems\alpha1.cpp
然后您可以这样做:
On the Windows command line, run this command (for example, from a batch file):
sfk fromclip +filter -rep x/x\x +toclip
Back in the editor, press Ctrl + V or Shift + Insert, pasting the result from the clipboard.
copy
和 move
是批处理命令之一,用于分别复制/粘贴和剪切/粘贴文件。我们在处理文件时不使用paste或cut术语,但如果我理解您的意思,需要将文件复制到另一个位置并将文件移动到另一个位置。