1

Is there a way to assign the clipboard value to a variable, then append the variable to new text with a batch file?

Here is a copy of what I'm attempting to do. In my example, "world" represents the value that the clipboard contains.

@echo off
set myclip=world
(
   echo hello %myclip%
)| clip
Squashman
  • 13,649
  • 5
  • 27
  • 36
crjunk
  • 949
  • 4
  • 21
  • 40
  • There are 3rd party solutions see: https://stackoverflow.com/a/17819928/4304 -or- if you can use powershell, there is a Get-Clipboard command. – zdan Feb 27 '18 at 17:51
  • Final Solution based on npocmaka's answer. ``for /f "usebackq tokens=* delims=" %%i in (`mshta "javascript:Code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text'))));"``) `do set "clipboardText=%%i" echo %clipboardText% echo|set/p=%clipboardText% It WORKS!|clip` – crjunk Feb 27 '18 at 21:55

2 Answers2

1

To get the clipboard data you can use this:

for /f "usebackq tokens=* delims=" %%i in (`mshta "javascript:Code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text'))));"`) do set "clipboardText=%%i"

echo %clipboardText%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • I tried the code you provided, but it does not seem to work. Does mshta need to be running before batch is executed? – crjunk Feb 27 '18 at 18:35
  • @crjunk - No. Is there any kind of error? I've just copied and pasted the code and it works form - prints the clipboard data on the console... – npocmaka Feb 27 '18 at 18:41
  • Wow. We use Sentinel One at work for threat protection. As soon as I executed the code it stopped the script and deleted it. Regardless of that I did see it echo the clipboard text. – Squashman Feb 27 '18 at 18:50
  • It's displaying "%%i was unexpected at this time." when I add Pause to the script – crjunk Feb 27 '18 at 19:05
  • @crjunk - execute this from bat file , but not from console directly. If you intend to use this from the console use single `%` instead of double. – npocmaka Feb 27 '18 at 19:50
  • Strange that it continued to give me that error after recreating the file multipe times. I ran it as a bat file each time. Now it is working properly. Maybe an odd character was pasted into the file and caused issues?? Thanks! – crjunk Feb 27 '18 at 21:54
0

Another way is using Powershell -command "Get-Clipboard":

@echo off

for /f "usebackq tokens=* delims=" %%i in (`powershell -command "Get-Clipboard"`) do set "myclip=%%i"

echo hello %myclip%

pause
amymor
  • 1
  • 1