Access clipboard in Windows batch file

Viewed 53237

Any idea how to access the Windows clipboard using a batch file?

11 Answers

Best way I know, is by using a standalone tool called WINCLIP .

You can get it from here: Outwit

Usage:

  • Save clipboard to file: winclip -p file.txt

  • Copy stdout to clipboard: winclip -c Ex: Sed 's/find/replace/' file | winclip -c

  • Pipe clipboard to sed: winclip -p | Sed 's/find/replace/'

  • Use winclip output (clipboard) as an argument of another command:

    FOR /F "tokens=* usebackq" %%G in ('winclip -p') Do (YOUR_Command %%G ) Note that if you have multiple lines in your clipboard, this command will parse them one by one.

You might also want to take a look at getclip & putclip tools: CygUtils for Windows but winclip is better in my opinion.

Multiple lines

The problem is resolved, but disappointment remains.

I was forced to split one command into two.

First of them well understands the text and service characters, but does not understand the backspace.

The second understands backspace, but does not understand many service characters.

Can anyone unite them?

Notepad ++, in the place where it should open, is commented out because sometimes the window does not get access to enter characters and therefore you need to make sure that it is active.

Of course, it is better to enter characters using the PID of the notebook process, but ...

The request from wmic opens for a long time, so do not close the notepad window until the bat file is closed.

    @echo off
    set "like=Microsoft Visual C++"
    set "flag=0"
    start /max C:\"Program Files\Notepad++\notepad++.exe" -nosession -multiInst
    ( set LF=^
    %= NEWLINE =%
    )
    set ^"NL=^^^%LF%%LF%^%LF%%LF%^^"
    ::------------------
    setlocal enabledelayedexpansion
    for /f "usebackq delims=" %%i in ( `wmic /node:"papa" product where "Name like '%%%like%%%'" get * ^| findstr /r /v "^$"`) do (
        for /f tokens^=1^ delims^=^" %%a in ("%%i") do set str=%%a
        if "!flag!"=="0" ( 
            ::start /max C:\"Program Files\Notepad++\notepad++.exe" -nosession -multiInst& set "flag=1" 
            for /f "delims=" %%i in ('mshta "javascript:new         ActiveXObject('WScript.Shell').SendKeys('{BS}{BS}{BS}{BS}');close(new         ActiveXObject('Scripting.FileSystemObject'));"') do set   
            set flag=1
        )
        @set /P "_=%%str%%"<NUL|clip
        for /f "delims=" %%i in ('mshta "javascript:new         ActiveXObject('WScript.Shell').SendKeys('^v');close(new         ActiveXObject('Scripting.FileSystemObject'));"') do set
        (echo %%NL%%)|clip  
        for /f "delims=" %%i in ('mshta "javascript:new         ActiveXObject('WScript.Shell').SendKeys('^v');close(new         ActiveXObject('Scripting.FileSystemObject'));"') do set 
    )
    setlocal disabledelayedexpansion

Here's one way with mshta:

@echo off
:printClip
for /f "usebackq tokens=* delims=" %%i in (
   `mshta "javascript:Code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text'))));"`
) do (
 echo cntent of the clipboard:
 echo %%i
)

the same thing for accessing it directly from console:

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