In the batch file that I am creating, I am trying to make a string comparison between two different copied text. Basically, is the old copied text the same as the newer copied text. For this I want to access the clipboard and store the old copied text in a variable inside my batch file. Then, copy the new text, store it in another variable inside my batch file, and compare the two and see if they are the same. I have tried searching for a solution, but all the articles that I have seen describe how to copy text into the clipboard using a batch file. Whereas, I am trying to do the opposite. All help is greatly appreciated.
Asked
Active
Viewed 3,438 times
2
-
in batch you can use swiss file knife (SFK) – Endoro Apr 15 '13 at 19:27
-
See [this answer](http://stackoverflow.com/a/15747067/1683264) for a way to retrieve text from the clipboard with a batch script. It will use PowerShell if available, or .NET otherwise. – rojo Apr 16 '13 at 00:28
2 Answers
2
Here is a short and batch embed solution:
1) create a small VB file which store the content of the clipboard in a temporary file
set clipFile=%temp%\clipboard
set vbFile=%temp%\clipboard.vbs
del %vbFile% %clipFile%
rem Create VB file
>"%vbFile%" (
echo.Set objHTML = CreateObject("htmlfile"^)
echo.ClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text"^)
echo.path = "%clipFile%"
echo.Set objFSO = CreateObject("Scripting.FileSystemObject"^)
echo.Set objFile = objFSO.OpenTextFile(path, 2, true^)
echo.objFile.WriteLine ClipboardText
echo.objFile.Close )>>"%vbFile%"
2) execute the VB script
"%vbFile%"
3) store file content in a variable
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (%clipFile%) do Set line=%%i
set clipboard=!line!
echo clipboard="%clipboard%"
ENDLOCAL

Jonathan
- 764
- 6
- 14
1
There's no native batch solution to read from the clipboard but there are some simple approaches using other languages.
Here's a Powershell script (which is easy to call from a batch file if you need to): http://poshcode.org/2150
Raymond Chen has a solution using perl: http://blogs.msdn.com/b/oldnewthing/archive/2009/11/10/9919908.aspx

Nate Hekman
- 6,507
- 27
- 30