如何将文本文件的内容复制到剪贴板?

How to copy the contents of a text file to the clipboard?

我使用下面的代码 VBS 来读取一个文本文件。如何将此文本文件的内容保存到我的计算机剪贴板以粘贴到其他地方?

Option Explicit
Dim objFileToRead, strFileText, content

Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close

这里有一个使用 clip 命令行的简单方法:

Option Explicit
Dim Title : Title = "Copy the contents of a text file to the clipboard"
Dim File2Read : File2Read = "C:.txt"
If CreateObject("Scripting.FileSystemObject").FileExists(File2Read) Then 
    CreateObject("WScript.Shell").Run "cmd.exe /c clip < "& File2Read &"",0,TRUE
    MsgBox "The contents of file : " & chr(34) & File2Read & chr(34) & vbCrlf & _
    " is copied to the clipboard !",vbInformation,Title
Else
    MsgBox "Check if the " & chr(34) & File2Read & chr(34) &" exists !",vbExclamation,Title
End If

编辑: 将 ping 测试保存到文件中并将其复制到剪贴板!

Option Explicit
Dim Title : Title = "Copy the contents of a text file to the clipboard"
Dim File2Read : File2Read = "E:\Yahoo_Ping.txt"
CreateObject("WScript.Shell").Run "CMD /C Ping www.yahoo.com > "& File2Read &"",1,True
If CreateObject("Scripting.FileSystemObject").FileExists(File2Read) Then 
    CreateObject("WScript.Shell").Run "CMD /U /C clip < "& File2Read &"",0,TRUE
    MsgBox "The contents of file : " & chr(34) & File2Read & chr(34) & vbCrlf & _
    " is copied to the clipboard !",vbInformation,Title
Else
    MsgBox "Check if the " & chr(34) & File2Read & chr(34) &" exists !",vbExclamation,Title
End If

使用 Batch 和 Powershell :

@echo off
Title Set-Clipboard and Get-ClipBoard with Powershell and Batch
Set "InputFile=%Temp%\%~n0.txt"
echo Please be patient ....
Ping www.whosebug.com > "%InputFile%"
Set "OutPutFile=%userprofile%\Desktop\MyClipBoard.txt"
Call :RunPS-Get-ClipBoard "%InputFile%" "%OutPutFile%"
Cls & Type "%OutPutFile%"
Pause & Exit
REM ----------------------------------------------------------------------
:RunPS-Get-ClipBoard <InputFile> <OutPutFile>
Set psCmd="&{Set-Clipboard -Value (GC '"%~1"') | Get-ClipBoard}"
If Exist "%~2" Del "%~2"
@for /F "tokens=*delims=" %%i in ('Powershell %psCmd%') do >> "%~2" echo %%i
Goto:eof
REM ----------------------------------------------------------------------