Saturday, February 18, 2012

Clear Clipboard Using VBScript

Clear Clipboard using VBScript

Write the below code in a notepad and save it as ".vbs" file.

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo. >NUL | clip", 0, True

To test the above code, copy some text [from notepad for example] and then run the above code. That copied text will disappear from clipboard and you will not be able to paste it.

Some facts about the above 2 lines of code:

Syntax for WshShell.Run:

WshShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn])

where 0 for "intWindowStyle" means hide the window and activate another window. TRUE for "bWaitOnReturn" means the Run method returns any error code returned by the application. [see more on WshShell.Run]

"cmd.exe" starts a new CMD shell. "/C" carries out "Some Command" and then terminates.

"echo." displays a blank line (echo on its own displays the current on/off status).

The space before the >NUL avoids any conflict with Windows NT/2000/XP redirection of STDOUT and STDERR which uses the syntax 1> and 2> respectively.

"Clip" allows you to copy the output of a command to the Windows clipboard. For example on the command prompt, type

C:\> echo test | clip

and after running the above command open new notepad and do Ctrl+v or Edit (menu)->Paste, it will paste "test" to that notepad.