在 Windows 批处理文件中访问剪贴板 [英] Access clipboard in Windows batch file
问题描述
知道如何使用批处理文件访问 Windows 剪贴板吗?
Any idea how to access the Windows clipboard using a batch file?
推荐答案
设置剪贴板的内容,如 Chris Thornton、klaatu 和 其他人 已经说过,使用 %windir%system32clip.exe
.
To set the contents of the clipboard, as Chris Thornton, klaatu, and bunches of others have said, use %windir%system32clip.exe
.
对于一个快速的单行,你可以这样做:
For a quick one-liner, you could do something like this:
powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"
如果需要,使用 for/F
循环捕获和解析.这不会像下面的 JScript 解决方案那样快速执行,但它确实具有简单性的优点.
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.
感谢 Jonathan 指出神秘的 htmlfile
COM 对象用于检索剪贴板.可以调用批处理 + JScript 混合来检索剪贴板的内容.实际上,只需要一行JScript,一行cscript
就可以触发,比之前提供的PowerShell/.NET方案要快很多.
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 a cscript
line to trigger it, and is much faster than the PowerShell / .NET solution offered earlier.
@if (@CodeSection == @Batch) @then
@echo off
setlocal
set "getclip=cscript /nologo /e:JScript "%~f0""
rem // If you want to process the contents of the clipboard line-by-line, use
rem // something like this to preserve blank lines:
for /f "delims=" %%I in ('%getclip% ^| findstr /n "^"') do (
setlocal enabledelayedexpansion
set "line=%%I" & set "line=!line:*:=!"
echo(!line!
endlocal
)
rem // If all you need is to output the clipboard text to the console without
rem // any processing, then remove the "for /f" loop above and uncomment the
rem // following line:
:: %getclip%
goto :EOF
@end // begin JScript hybrid chimera
WSH.Echo(WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text'));
旧解决方案:
可以使用 .NET 从 Windows 控制台检索剪贴板文本,而无需任何 3rd 方应用程序.如果您安装了 powershell
,您可以通过创建一个虚构的文本框并将其粘贴到其中来检索剪贴板内容.(来源)
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)
Add-Type -AssemblyName System.Windows.Forms
$tb = New-Object System.Windows.Forms.TextBox
$tb.Multiline = $true
$tb.Paste()
$tb.Text
如果您没有 powershell
,您仍然可以编译一个简单的 .NET 应用程序来将剪贴板文本转储到控制台.这是一个 C# 示例.(灵感)
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)
using System;
using System.Threading;
using System.Windows.Forms;
class dummy {
[STAThread]
public static void Main() {
if (Clipboard.ContainsText()) Console.Write(Clipboard.GetText());
}
}
这是一个结合了这两种方法的批处理脚本.如果 powershell
存在于 %PATH%
中,请使用它.否则,找到 C# 编译器/链接器并构建一个临时的 .NET 应用程序.正如您在批处理脚本注释中看到的那样,您可以使用 for/f
循环捕获剪贴板内容,或者简单地将它们转储到控制台.
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 a for /f
loop or simply dump them to the console.
:: clipboard.bat
:: retrieves contents of clipboard
@echo off
setlocal enabledelayedexpansion
:: Does powershell.exe exist within %PATH%?
for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
set getclip=powershell "Add-Type -AssemblyName System.Windows.Forms;$tb=New-Object System.Windows.Forms.TextBox;$tb.Multiline=$true;$tb.Paste();$tb.Text"
) else (
rem :: If not, compose and link C# application to retrieve clipboard text
set getclip=%temp%getclip.exe
>"%temp%c.cs" echo using System;using System.Threading;using System.Windows.Forms;class dummy{[STAThread]
>>"%temp%c.cs" echo public static void Main^(^){if^(Clipboard.ContainsText^(^)^) Console.Write^(Clipboard.GetText^(^)^);}}
for /f "delims=" %%I in ('dir /b /s "%windir%microsoft.net*csc.exe"') do (
if not exist "!getclip!" "%%I" /nologo /out:"!getclip!" "%temp%c.cs" 2>NUL
)
del "%temp%c.cs"
if not exist "!getclip!" (
echo Error: Please install .NET 2.0 or newer, or install PowerShell.
goto :EOF
)
)
:: If you want to process the contents of the clipboard line-by-line, use
:: something like this to preserve blank lines:
for /f "delims=" %%I in ('%getclip% ^| findstr /n "^"') do (
set "line=%%I" & set "line=!line:*:=!"
echo(!line!
)
:: If all you need is to output the clipboard text to the console without
:: any processing, then remove the above "for /f" loop and uncomment the
:: following line:
:: %getclip%
:: Clean up the mess
del "%temp%getclip.exe" 2>NUL
goto :EOF
这篇关于在 Windows 批处理文件中访问剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!