Anonymous
Anonymous

Reputation: 61

Batch or VBS copy to clipboard

I am looking for a way to copy the text "Hello world" to the clipboard using either VBS or batch. I've done a lot of research but couldn't find anything.

Upvotes: 1

Views: 3719

Answers (3)

Hackoo
Hackoo

Reputation: 18857

You can do it with an html object to retrieve the contents of the clipboard:

' Get clipboard text
Set objHTML = CreateObject("htmlfile")
Set Ws = CreateObject("WScript.Shell")
Clipboardtext = objHTML.ParentWindow.ClipboardData.GetData("text")
MsgBox Clipboardtext,vbInformation,"Get Clipboard"

sText = "Hello World"
'Here we set the string sText into Clipboard
Ws.Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(sText, "\", "\\"), "'", "\'") & "');close();""", 0, True

Upvotes: 1

npocmaka
npocmaka

Reputation: 57322

As Squashman proposed you can use :

echo string|clip

thought this will set one enter at the end of the string. To strip the enter you can use this:

mshta "javascript:Code(close(clipboardData.setData('text','string')));"

Upvotes: 1

iBug
iBug

Reputation: 37317

I'm afraid this isn't easily achievable using batch or VBScript.

To access clipboard, you need to use a series of Windows APIs, which is not directly possible with either batch or VBScript. Your best bet could be writing a CLI program (helper program), then call it in yout batch / VBS.

Upvotes: 0

Related Questions