在 Windows 批处理文件中访问剪贴板
知道如何使用批处理文件访问 Windows 剪贴板吗?
Any idea how to access the Windows clipboard using a batch file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
知道如何使用批处理文件访问 Windows 剪贴板吗?
Any idea how to access the Windows clipboard using a batch file?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
精简它(在足够新的 Windows 版本上):
powershell
commandlet。CLIPBOARD_TEXT
环境变量中(cmd.exe
执行bash
样式反引号的最接近方法`
capture)更新 2017-12-04:
感谢 @Saintali 指出 PowerShell 5.0 添加了
Get-Clipboard
作为顶层cmdlet,因此现在它可以作为单行:for /f "eol=; tokens=*" %I in ('powershell Get-Clipboard') do set CLIPBOARD_TEXT=%I
Slimming it down (on a new enough version of Windows):
powershell
commandlet.CLIPBOARD_TEXT
enviroment variable (cmd.exe
's closest way to dobash
style backtick`
capture)Update 2017-12-04:
Thanks to @Saintali for pointing out that PowerShell 5.0 adds
Get-Clipboard
as a top level cmdlets, so this now works as a one liner:for /f "eol=; tokens=*" %I in ('powershell Get-Clipboard') do set CLIPBOARD_TEXT=%I
Clip 命令可以很好地将文本通过管道传输到剪贴板,但它无法从剪贴板读取。 vbscript / javascript 中有一种方法可以读取/写入剪贴板,但它使用自动化和一个不可见的实例(如果 Internet Explorer 执行此操作),因此它非常丰富。
我发现用于通过脚本操作剪贴板的最佳工具是 Nirsoft 的免费 NirCmd 工具。
http://www.nirsoft.net/utils/nircmd.html
它就像瑞士人批处理命令的军刀全部在一个小 .exe 文件中。对于剪贴板命令,您会说类似
nircmd Clipboard [Action] [Parameter]
另外,您可以使用 ~$clipboard$ 变量作为参数直接引用其任何命令中的剪贴板内容。 Nircmd 中还包含用于运行其他程序或命令的命令,因此可以使用它以这种方式将剪贴板内容作为参数传递给其他批处理命令。
剪贴板操作:
请注意,大多数程序始终会向剪贴板写入纯文本副本,即使它们正在向剪贴板写入特殊的 RTF 或 HTML 副本,但这些程序是使用不同的剪贴板格式类型作为内容写入的,因此您可能无法访问这些格式,除非您的程序明确请求剪贴板中的该类型的数据。
The clip command is good to pipe text to the clipboard, but it can't read from the clipboard. There is a way in vbscript / javascript to read / write the clipboard but it uses automation and an invisible instance if Internet Explorer to do it so its pretty fat.
The best tool I've found for working the clipboard from script is Nirsoft's free NirCmd tool.
http://www.nirsoft.net/utils/nircmd.html
Its like a swiss army knife of batch commands all in one small .exe file. For clipboard commands you would say someting like
nircmd clipboard [Action] [Parameter]
Plus you can directly refer to clipboard contents in any of its commands using its ~$clipboard$ variable as an argument. Nircmd also has commands in it to run other programs or commands from so it is possible to use it to pass the clipboard contents as an argument to other batch commands this way.
Clipboard actions:
Note that most programs will always write a plain text copy to the clipboard even when they are writing a special RTF or HTML copy to the clipboard but those are written as content using a different clipboard format type so you may not be able to access those formats unless your program explicitly requests that type of data from the clipboard.
我知道的最好方法是使用名为 WINCLIP 的独立工具。
您可以从这里获取它:Outwit
用法:
将剪贴板保存到文件:
winclip -p file.txt
将标准输出复制到剪贴板:
winclip -c
例如:Sed 's/find/replace/'文件| winclip -c
通过管道将剪贴板传输到 sed:
winclip -p | sed 's/find/replace/'
使用 winclip 输出(剪贴板)作为另一个命令的参数:
FOR /F "tokens=* usebackq" %%G in ('winclip -p') Do (YOUR_Command %%G )
请注意,如果剪贴板中有多行,则此命令将一一解析。您可能还想看看getclip & putclip 工具:CygUtils for Windows 但 winclip 更好我的意见。
Best way I know, is by using a standalone tool called WINCLIP .
You can get it from here: Outwit
Usage:
Save clipboard to file:
winclip -p file.txt
Copy stdout to clipboard:
winclip -c
Ex:Sed 's/find/replace/' file | winclip -c
Pipe clipboard to sed:
winclip -p | Sed 's/find/replace/'
Use winclip output (clipboard) as an argument of another command:
FOR /F "tokens=* usebackq" %%G in ('winclip -p') Do (YOUR_Command %%G )
Note that if you have multiple lines in your clipboard, this command will parse them one by one.You might also want to take a look at getclip & putclip tools: CygUtils for Windows but winclip is better in my opinion.
这是使用 mshta 的一种方法:
直接从控制台访问它的方法相同:
Here's one way with mshta:
the same thing for accessing it directly from console:
在 win 11 中运行的示例批处理文件,我必须阅读本主题中的几个答案才能找到解决方案并获取剪贴板。具体应用是YouTube下载剪贴板中的URL。
An example batch file that is working in win 11, I had to read several answers in this topic to find the solution and get the clipboard. The specific application is YouTube download of the URL in the clipboard.
在 Vista 或更高版本中,它是内置的。只需将输出通过管道传输到“clip”程序即可。
这是我写的一篇文章:
http://www.clipboardextender.com /general-clipboard-use/command-window-output-to-clipboard-in-vista
这篇文章还包含一个名为 Dos2Clip 的免费实用程序(我认为是我编写的)的链接,该实用程序可以在 XP 上使用。
编辑:我发现我已经把问题搞反了,我的解决方案输出到剪贴板,没有读取它。对不起!
更新:与 Dos2Clip 一起的是 Clip2Dos(在同一 zip 中),它将把剪贴板文本发送到标准输出。所以这应该对你有用。 Pascal 源代码包含在 zip 中。
With Vista or higher, it's built in. Just pipe output to the "clip" program.
Here's a writeup (by me):
http://www.clipboardextender.com/general-clipboard-use/command-window-output-to-clipboard-in-vista
The article also contains a link to a free utility (written by Me, I think) called Dos2Clip, which can be used on XP.
EDIT: I see that I've gotten the question backwards, my solution OUTPUTS to the clipboard, doesn't read it. sorry!
Update: Along with Dos2Clip, is Clip2Dos (in the same zip), which will send the clipboard text to stdout. So this should work for you. Pascal source is included in the zip.
要从批处理脚本中检索剪贴板内容:不存在“纯粹的”批处理解决方案。
如果您想要嵌入 100% 批处理解决方案,则需要从批处理中生成其他语言文件。
这是一个简短的和批量嵌入解决方案,生成 VB 文件(通常安装在大多数 Windows 上)
To retrive clipboard content from your batch script: there is no "pure" batch solution.
If you want an embed 100% batch solution, you will need to generate the other language file from your batch.
Here is a short and batch embed solution which generate a VB file (Usually installed on most windows)
由于所有答案都令人困惑,这里我的代码没有延迟或额外的窗口来打开从剪贴板复制的流链接:
Since all answers are confusing, here my code without delays or extra windows to open stream link copied from clipboard:
正如其他人所说,将管道输出到剪贴板是由clip 提供的。要从剪贴板读取输入,请使用此捆绑包中的 pclip 工具。那里还有很多其他好东西。
例如,您正在学习在线教程,并且想要使用剪贴板的内容创建一个文件...
或者您想要执行复制的命令...
Piping output to the clipboard is provided by clip, as others have said. To read input from the clipboard, use the pclip tool in this bundle. And there's tons of other good stuff in there.
So for example, you're going through an online tutorial and you want to create a file with the contents of the clipboard...
or you want to execute a copied command...
多行
问题已解决,但失望依然存在。
我被迫将一个命令分成两个。
首先,他们很好地理解文本和服务字符,但不理解退格键。
第二个理解退格键,但不理解许多服务字符。
有人能团结他们吗?
Notepad ++ 在应打开的位置被注释掉,因为有时窗口无法输入字符,因此您需要确保它处于活动状态。
当然,最好使用笔记本进程的PID来输入字符,但是......
来自wmic的请求打开了很长一段时间,所以在bat文件关闭之前不要关闭记事本窗口。
Multiple lines
The problem is resolved, but disappointment remains.
I was forced to split one command into two.
First of them well understands the text and service characters, but does not understand the backspace.
The second understands backspace, but does not understand many service characters.
Can anyone unite them?
Notepad ++, in the place where it should open, is commented out because sometimes the window does not get access to enter characters and therefore you need to make sure that it is active.
Of course, it is better to enter characters using the PID of the notebook process, but ...
The request from wmic opens for a long time, so do not close the notepad window until the bat file is closed.
还有另一种解决方案,这是一种解决方法,但很简单。您可以从用户获取输入字符串,而不是直接获取剪贴板 - 用户可以从 cmd 窗口粘贴文本(右键单击 cmd 窗口)然后选择粘贴或使用热键:Alt+Space 然后按 E 然后按 P,简而言之,Alt+Space -> E+P)
bat 脚本为:
步骤 0:复制您需要的文本。
步骤1:运行上面的bat脚本。
步骤2:Alt+空格-> E+P,您应该看到剪贴板中的文本已显示,然后按 Enter。完毕!
There is another solution, it's a workaround but simple. Instead of get clipboard directly, you can get input string from user - user can paste the text from cmd window (right click the cmd window then select paste or use hotkey: Alt+Space then press E then press P, in short, Alt+Space -> E+P)
The bat script is:
Step 0: copy the text that you need.
Step 1: run the bat script above.
Step 2: Alt+Space -> E+P, you should see the text from clipboard is shown, press Enter. Done!
这可能不是确切的答案,但它会对您的任务有所帮助。
原帖:
访问https://groups.google.com/d/msg/ alt.msdos.batch/0n8icUar5AM/60uEZFn9IfAJ
提问者罗杰·亨特
回答者 William Allen
更简洁的步骤:
步骤 1)使用任何文本编辑器在桌面上创建一个名为 Copy.bat 的“bat”文件,然后复制并粘贴以下代码并保存。
步骤 2) 创建一个空白文本文件(在我的例子中为 H 驱动器中的“CLIP.txt”)或任何位置,确保更新“FN=H:\CLIP”下 Copy.bat 文件中的路径.txt' 与您的目标文件路径。
就是这样。
因此,基本上,当您从任何地方复制任何文本并从桌面运行 Copy.bat 文件时,它会使用其中的剪贴板内容更新 CLIP.txt 文件并保存它。
用途:
我用它从远程连接的计算机传输数据,其中不同连接之间禁用复制/粘贴;其中共享驱动器 (H:) 对于所有连接都是通用的。
This might not be the exact answer, but it will be helpful for your Quest.
Original post:
Visit https://groups.google.com/d/msg/alt.msdos.batch/0n8icUar5AM/60uEZFn9IfAJ
Asked by Roger Hunt
Answered by William Allen
Much Cleaner Steps:
Step 1) create a 'bat' file named Copy.bat in desktop using any text editors and copy and past below code and save it.
Step 2) create a Blank text file (in my case 'CLIP.txt' in H Drive) or anywhere, make sure you update the path in Copy.bat file under 'FN=H:\CLIP.txt' with your destination file path.
That's it.
So, basically when you copy any text from anywhere and run Copy.bat file from desktop, it updates CLIP.txt file with the Clipboard contents in it and saves it.
Uses:
I use it to transfer data from remotely connected machines where copy/paste is disabled between different connections; where shared drive (H:) is common to all Connections.
要设置剪贴板的内容,如 Chris Thornton、klaatu 和一堆其他人说过,使用
%windir%\system32\clip.exe
。更新 2:
对于快速的单行代码,您可以执行以下操作:
如果需要,使用
for /F
循环捕获和解析。这不会像下面的 JScript 解决方案执行得那么快,但它确实具有简单的优点。更新的解决方案:
感谢 Jonathan 指出神秘的
htmlfile
COM 对象用于检索剪贴板。可以调用批处理 + JScript 混合来检索剪贴板的内容。事实上,它只需要一行 JScript 和一行 cscript 即可触发它,并且比之前提供的 PowerShell / .NET 解决方案要快得多。旧的解决方案:
可以使用 .NET 从 Windows 控制台检索剪贴板文本,而无需任何第三方应用程序。如果您安装了
powershell
,您可以通过创建一个虚构的文本框并将其粘贴到其中来检索剪贴板内容。 (来源)如果您不这样做如果没有
powershell
,您仍然可以编译一个简单的 .NET 应用程序以将剪贴板文本转储到控制台。这是一个 C# 示例。 (灵感)这是一个结合了这两种方法的批处理脚本。如果
%PATH%
中存在powershell
,请使用它。否则,找到 C# 编译器/链接器并构建临时 .NET 应用程序。正如您在批处理脚本注释中看到的,您可以使用for /f
循环捕获剪贴板内容,或者简单地将它们转储到控制台。To set the contents of the clipboard, as Chris Thornton, klaatu, and bunches of others have said, use
%windir%\system32\clip.exe
.Update 2:
For a quick one-liner, you could do something like this:
Capture and parse with a
for /F
loop if needed. This will not execute as quickly as the JScript solution below, but it does have the advantage of simplicity.Updated solution:
Thanks Jonathan for pointing to the capabilities of the mysterious
htmlfile
COM object for retrieving the clipboard. It is possible to invoke a batch + JScript hybrid to retrieve the contents of the clipboard. In fact, it only takes one line of JScript, and acscript
line to trigger it, and is much faster than the PowerShell / .NET solution offered earlier.Old solution:
It is possible to retrieve clipboard text from the Windows console without any 3rd-party applications by using .NET. If you have
powershell
installed, you can retrieve the clipboard contents by creating an imaginary textbox and pasting into it. (Source)If you don't have
powershell
, you can still compile a simple .NET application to dump the clipboard text to the console. Here's a C# example. (Inspiration)Here's a batch script that combines both methods. If
powershell
exists within%PATH%
, use it. Otherwise, find the C# compiler / linker and build a temporary .NET application. As you can see in the batch script comments, you can capture the clipboard contents using afor /f
loop or simply dump them to the console.