如何通过命令行将文件(而不是内容)复制/剪切到Windows中的剪贴板?
有没有办法从命令行将文件复制(或剪切)到Windows剪贴板 尤其是批处理脚本。我知道如何将内容复制到剪贴板(如何通过命令行将文件(而不是内容)复制/剪切到Windows中的剪贴板?,windows,shell,command-line,batch-file,clipboard,Windows,Shell,Command Line,Batch File,Clipboard,有没有办法从命令行将文件复制(或剪切)到Windows剪贴板 尤其是批处理脚本。我知道如何将内容复制到剪贴板(键入file | clip),但事实并非如此。我希望在Windows资源管理器中按Ctrl+C组合键时保存整个文件。您可以尝试(SFK): 示例:将反斜杠改为正斜杠。假设您在中打开了以下文本: 出于某种原因,第一行的格式如下: foo\bar\systems\alpha1.cpp 然后你可以这样做: 使用SHIFT+光标键标记第一行 按Ctrl+C或Ctrl+Insert将其复制到剪
键入file | clip
),但事实并非如此。我希望在Windows资源管理器中按Ctrl+C组合键时保存整个文件。您可以尝试(SFK):
示例:将反斜杠改为正斜杠。假设您在中打开了以下文本: 出于某种原因,第一行的格式如下:
foo\bar\systems\alpha1.cpp
然后你可以这样做:
如您所见,该行更改为“foo\bar\systems\alpha1.cpp”。
copy
和move
分别是复制/粘贴和剪切/粘贴文件的批处理命令。在处理文件时,我们不使用“粘贴”或“剪切”这两个术语,但如果我理解您的意思,则需要将文件复制到另一个位置并将文件移动到另一个位置。这会将文件的内容放入剪贴板(通过clip.exe完成)
要获取实际的文件,您可能必须求助于其他编程语言,如VBScript或PowerShell来访问Windows API。我不完全确定当你按住CTRL+C键的时候,浏览器会把什么放到剪贴板上。我怀疑它使用通知系统做的事情比将文件路径放在那里更智能。根据CTRL+V的上下文,您会得到一些东西(资源管理器、Word)或什么都没有(记事本)。好的,似乎最简单的方法是创建一个小型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);
}
}
}
使用系统;
使用System.Windows.Forms;
使用System.Collections.Generic;
使用System.Collections.Specialized;
命名空间File2Clip
{
公共类应用程序
{
[状态线程]
静态void Main(字符串[]参数)
{
列表=新列表();
弦线;
而(!string.IsNullOrEmpty(line=Console.ReadLine())list.Add(line);
foreach(args中的字符串s)列表。添加(s);
StringCollection路径=新建StringCollection();
foreach(列表中的字符串s){
控制台。写入;
路径。添加(
System.IO.Path.IsPathRooted(多个)?
s:
System.IO.Directory.GetCurrentDirectory()+
@“\”+s);
}
剪贴板.SetFileDropList(路径);
}
}
}
2017年编辑:这里有一个包含源代码和二进制代码的版本。您可以使用。我一直希望在Emacs中使用这个版本,因此,受这个问题、答案和大量NIH综合征的启发,我在
picellif还处理通配符(我不清楚rostok的C#版本是否处理通配符)。我也遇到了类似的问题,为此我编写了一个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 type %ext%
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
/*
MsgBox,
(
%encoding%,
%fileSize%,
-----------------------------
%fileContent%
)
*/
编译后,只需CopyToClip.exe
即可将文件内容复制到剪贴板,目前支持文本和图像
最后,我们可以在右击菜单上添加快捷菜单
您需要将D:\\Program Files\\CopyToClip\\CopyToClip.exe更改为您自己的路径
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*]
[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\aCopy to Clip]
"Icon"="C:\\Windows\\System32\\imageres.dll,14"
@="Copy to Clip"
[HKEY_CLASSES_ROOT\*\shell\aCopy to Clip\Command]
@="D:\\Program Files\\CopyToClip\\CopyToClip.exe \"%1\""
您应该签出Windows Powershell
,您可以对其使用cp
命令
Windows中的简单命令行复制
通过在cmd
中键入Powershell
打开Powershell
然后执行此操作cp path_to_file/folder_to_copy path_to_destination
一些例子
正在将文件复制到您所在的目录中!
cp路径到文件。
这里的
表示当前目录
复制到受保护的目录
如果要将文件复制到受限制的文件夹中,则需要管理员权限查看@cb0此处。请查看链接到线程的链接。否的可能副本,这与文件复制无关(复制、xcopy、移动等都是非常基本的)。问题是如何将文件放入系统剪贴板。确定。我想,如果我们了解了文件的类型以及它是如何被使用的,可能会有人有办法提供帮助。谢谢,这太棒了,我一直在寻找类似的东西很长时间了。顺便说一句,我花了两天的时间在vb中思考是否可以这样做,直到意识到我已经在C:\Windows\Microsoft.NET
下面的某个地方找到了C
编译器。如果其他答案都不能回答你的问题,你应该接受这个答案,即使它是你自己的,这样我们其他人就可以更容易地找到答案。好的解决方案。为了便于使用,我将此实用程序作为fileclip.exe
添加到系统路径中。从路径使用它,我必须将System.IO.path.GetDirectoryName(Environment.GetCommandLineArgs()[0])
更改为System.IO.Directory.GetCurrentDirectory()
用于相对路径。一些powershell等价物:我也希望它用于Emacs和其他命令行使用:)我一直在使用上面rostok答案中的C代码,但是如果你有windows二进制文件,我很乐意使用它,因为你写的是Emacs使用的。@Misalevable github有windows二进制文件(查看“发布”选项卡下的内容)。过去三周我一直在使用0.2.0,没有注意到错误。哎呀,我不知道我怎么会错过它。现在你又有一个用户:)嗨,罗伊只是想让你知道这非常有用,我使用的是相同的名称picellif,希望有朝一日能记住它的名称,而不总是这样
type \path\to\file|clip
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);
}
}
}
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 type %ext%
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
/*
MsgBox,
(
%encoding%,
%fileSize%,
-----------------------------
%fileContent%
)
*/
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*]
[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\aCopy to Clip]
"Icon"="C:\\Windows\\System32\\imageres.dll,14"
@="Copy to Clip"
[HKEY_CLASSES_ROOT\*\shell\aCopy to Clip\Command]
@="D:\\Program Files\\CopyToClip\\CopyToClip.exe \"%1\""