In a batch script, I wish to save the clipboard text into a variable. Having searched it seems that this is not possible without 3rd-party tools. Well I have nircmd, and one of its commands is:
"consolewrite [text] Sends the specified text to the standard output (stdout)."
I was hoping I could achieve my goal with the following straightforward line:
FOR /F "tokens=* USEBACKQ" %%F IN (`nircmd.exe consolewrite ~$clipboard$`) DO (SET varCB=%%F)
which in theory would see nircmd write the clipboard contents to STDOUT, and then the for command would save this output into varCB. But it doesn't work.
UPDATE: Actually that line now works. It will place the text in the clipboard into varCB. Must have made some error in my initial testing.
UPDATE 2: It has been pointed out that if there are multiple lines of text on the clipboard, the above code will only store the last line in the variable varCB. If your clipboard text has multiple lines you can store each line in its own variable with the following code:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`nircmd.exe consolewrite ~$clipboard$`) DO (
SET varCB!count!=%%F
SET /a count=!count!+1
)
ECHO %varCB1%
ECHO %varCB2%
ECHO %varCB3%
[...]
ENDLOCAL
(adapted from a code I copied long ago, I forget from where unfortunately.)
UPDATE 3: It has also been pointed out that my use of the term "3rd-party tools" above is inaccurate. I simply meant that other applications or scripting languages would be needed to achieve the goal (such as PowerShell or NirCMD). Of course Some of these tools might already be included with Win10 and so are not technically "3rd-party". Sorry about that!