2

I've tried a few things, from WScript.Echo "cls" to crazy functions like the ones below I found on the internet.

Set CrlScrn = WScript.CreateObject("WScript.Shell") 
CrlScrn.SendKeys "CLS + {ENTER}" 

and

Set CrlScrn = WScript.CreateObject("WScript.Shell") 
CrlScrn.Run "cmd echo /c cls" 
Set CrlScrn = Nothing 

Neither of these work.

I'm executing my vbs simply by running cscript myscript.vbs so I can view output in the console. Any other ideas on how to clear the screen?

  • Not possible from within `cscript` (unless you want to write many blank lines). What is the purpose of doing this (i.e. what do you want to clear the screen for)? Have you considered using HTA or PowerShell instead of VBScript in a command prompt? – Ansgar Wiechers May 05 '15 at 21:10
  • It's really just for aesthetics. I'm logging progress on a long-running script every 30 seconds or so. Instead of it adding a new line I just wanted it to be at the top of the console or something. Oh well. –  May 05 '15 at 21:19

1 Answers1

0

With the help from How to use ANSI escape sequences with CSCRIPT on Windows 10?

For me it worked in VBScript (on CScript using cmd), with these 2 lines:


    'move cursor to beggining of cmd screen
    WScript.StdOut.Write Chr(27) & "[34A"
    'clear screen
    WScript.StdOut.Write Chr(27) & "[2J"

For the first line I used the reference in MS Docs: Console Virtual Terminal Sequences The second line was used since Win95 by using Ansi.sys according to Tom Lavedas' answer in link

but now I had to add the ']' character to the sequence (as in ms doc)


BTW first I had to add this value to the registry:

[HKEY_CURRENT_USER\Console]
"VirtualTerminalLevel"=dword:00000001

...And I didn't need to use the powershell hack (by adding this to the beggining on the script) (also in the referenced stackoverflow thread)


var ps = WScript.CreateObject("WScript.Shell").Exec("powershell.exe -nop -ep Bypass -c \"exit\"");
while (ps.Status == 0) WScript.Sleep(50);