在Windowsbatch file中访问剪贴板

任何想法如何使用batch file访问Windows剪贴板?

要设置剪贴板的内容,正如Chris Thornton,klaatu和其他人所说的,使用%windir%\system32\clip.exe


更新2:

对于快速的一行,你可以做这样的事情:

 powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()" 

如果需要,用for /F循环捕获和parsing。 这不会像下面的JScript解决scheme那样快,但它具有简单性的优点。


更新解决scheme

感谢Jonathan指出神秘的htmlfile COM对象检索剪贴板的能力。 可以调用批+ JScript混合来检索剪贴板的内容。 实际上,它只需要一行JScr​​ipt和一个cscript行来触发,而且比之前提供的PowerShell / .NET解决scheme要快得多。

 @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')); 

旧解决scheme:

通过使用.NET,无需任何第三方应用程序就可以从Windows控制台检索剪贴板文本。 如果你安装了powershell ,你可以通过创build一个虚构的文本框并粘贴到其中来获取剪贴板的内容。 ( 来源 )

 Add-Type -AssemblyName System.Windows.Forms $tb = New-Object System.Windows.Forms.TextBox $tb.Multiline = $true $tb.Paste() $tb.Text 

如果你没有powershell ,你仍然可以编译一个简单的.NET应用程序来将剪贴板文本转储到控制台。 这是一个C#示例。 ( 灵感 )

 using System; using System.Threading; using System.Windows.Forms; class dummy { [STAThread] public static void Main() { if (Clipboard.ContainsText()) Console.Write(Clipboard.GetText()); } } 

这是一个结合了两种方法的批处理脚本。 如果在%PATH%内存在powershell ,请使用它。 否则,findC#编译器/链接器并构build一个临时的.NET应用程序。 正如您在批处理脚本注释中看到的那样,您可以使用for /f循环捕获剪贴板内容for /f或者直接将其转储到控制台。

 :: 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 

Vista或更高版本,内置。只需输出到“剪辑”程序。 这里是一个文件(由我): http ://www.clipboardextender.com/general-clipboard-use/command-window-output-to-clipboard-in-vista文章还包含一个免费的实用工具的链接(由我认为)称为Dos2Clip,可以在XP上使用。

编辑:我看到,我已经把问题倒退了,我的解决scheme输出到剪贴板,不读它。 抱歉!

更新:随着Dos2Clip,是Clip2Dos(在同一个zip),这将发送剪贴板文本到标准输出。 所以这应该为你工作。 Pascal源代码包含在zip中。

剪辑命令是很好的pipe道文本到剪贴板,但它不能从剪贴板中读取。 有一种方法在VBScript / JavaScript读/写剪贴板,但它使用自动化和一个不可见的实例,如果Internet Explorer来做到这一点,它非常胖。

我发现从脚本工作剪贴板的最佳工具是Nirsoft的免费NirCmd工具。

http://www.nirsoft.net/utils/nircmd.html

它像一个瑞士军刀的批处理命令都在一个小的.exe文件。 对于剪贴板命令,你会说有点像

nircmd剪贴板[操作] [参数]

另外,您可以使用〜$剪贴板$variables作为参数直接引用任何命令中的剪贴板内容。 Nircmd也有命令来运行其他程序或命令,所以可以用这种方式将剪贴板内容作为parameter passing给其他批处理命令。

剪贴板操作:

  set - set the specified text into the clipboard. readfile - set the content of the specified text file into the clipboard. clear - clear the clipboard. writefile - write the content of the clipboard to a file. (text only) addfile - add the content of the clipboard to a file. (text only) saveimage - Save the current image in the clipboard into a file. copyimage - Copy the content of the specified image file to the clipboard. saveclp - Save the current clipboard data into Windows .clp file. loadclp - Load Windows .clp file into the clipboard. 

请注意,大多数程序将总是写一个纯文本副本到剪贴板,即使他们正在写一个特殊的RTF或HTML副本到剪贴板,但这些写入内容使用不同的剪贴板格式types,所以你可能无法访问这些格式除非你的程序明确地向剪贴板请求这种types的数据。

减肥(在Windows的新版本上):

 set _getclip=powershell "Add-Type -Assembly PresentationCore;[Windows.Clipboard]::GetText()" for /f "eol=; tokens=*" %I in ('%_getclip%') do set CLIPBOARD_TEXT=%I 
  1. 第一行声明了一个powershell命令行开关。
  2. 第二行运行并捕获此命令行的控制台输出到CLIPBOARD_TEXT环境variables( cmd.exe是最接近bash风格的反向捕获方式)

从批处理脚本中检索剪贴板内容:没有“纯”批处理解决scheme。

如果您想要embedded100%批处理解决scheme,则需要从批处理中生成其他语言文件。

这里是一个简短的批量embedded解决scheme,它生成一个VB文件(通常安装在大多数窗口)

这可能不是确切的答案,但它会帮助你的任务。

原帖:访问https://groups.google.com/d/msg/alt.msdos.batch/0n8icUar5AM/60uEZFn9IfAJ Roger Hunt 提问 William Allen提供


更清洁的步骤:

步骤1)使用任何文本编辑器在桌面上创build一个名为Copy.bat的“bat”文件,并复制和过滤下面的代码并保存。

  @ECHO OFF SET FN=%1 IF ()==(%1) SET FN=H:\CLIP.TXT :: Open a blank new file REM New file>%FN% ECHO.set sh=WScript.CreateObject("WScript.Shell")>_TEMP.VBS ECHO.sh.Run("Notepad.exe %FN%")>>_TEMP.VBS ECHO.WScript.Sleep(200)>>_TEMP.VBS ECHO.sh.SendKeys("^+{end}^{v}%%{F4}{enter}{enter}")>>_TEMP.VBS cscript//nologo _TEMP.VBS ECHO. The clipboard contents are: TYPE %FN% :: Clean up DEL _TEMP.VBS SET FN= 

步骤2)创build一个空白文本文件 (在我的情况下,在H驱动器'CLIP.txt')或任何地方,请确保您更新'FN = H:\ CLIP.txt'下Copy.bat文件的path与您的目标文件path。

而已。

所以,基本上当你从任何地方复制任何文本,并从桌面运行Copy.bat文件时,它将Clipboard内容更新为CLIP.txt文件并保存

用途:

我使用它来从远程连接的机器上传输数据,其中在不同连接之间禁用复制/粘贴; 共享驱动器(H :)对所有连接都是共同的。

正如其他人所说的,pipe道输出剪贴板是由剪辑提供的。 要从剪贴板读取input,请使用此捆绑中的pclip工具。 那里还有很多其他好东西。

所以,例如,你正在通过一个在线教程,你想创build一个文件与剪贴板的内容…

 c:\>pclip > MyNewFile.txt 

或者你想执行一个复制的命令…

 c:\>pclip | cmd 

由于所有的答案都令人困惑,在这里我的代码没有延迟或额外的窗口打开stream从剪贴板复制链接:

 @ECHO OFF //Name of TEMP TXT Files SET TXTNAME=CLIP.TXT //VBA SCRIPT :: VBS SCRIPT ECHO.Set Shell = CreateObject("WScript.Shell")>_TEMP.VBS ECHO.Set HTML = CreateObject("htmlfile")>>_TEMP.VBS ECHO.TXTPATH = "%TXTNAME%">>_TEMP.VBS ECHO.Set FileSystem = CreateObject("Scripting.FileSystemObject")>>_TEMP.VBS ECHO.Set File = FileSystem.OpenTextFile(TXTPATH, 2, true)>>_TEMP.VBS ECHO.File.WriteLine HTML.ParentWindow.ClipboardData.GetData("text")>>_TEMP.VBS ECHO.File.Close>>_TEMP.VBS cscript//nologo _TEMP.VBS :: VBS CLEAN UP DEL _TEMP.VBS SET /p streamURL=<%TXTNAME% DEL %TXTNAME% :: 1) The location of Player SET mvpEXE="D:\Tools\Programs\MVP\mpv.com" :: Open stream to video player %mvpEXE% %streamURL% @ECHO ON