Sign in to follow this  
Followers 0
Draygoes

Clearing a cmd screen with consolewrite()?

28 posts in this topic

Just what the title says, I need a command to clear the cmd prompt screen through somthing like consolewrite().

This is a command line app, and I would like to flush the screen every now and then. Is it possible? Or should it go in feature requests?


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites



Command prompt screen? How are you introducing the Command line text? GUI, standard system cmd window, etc...

Are you using some sort of Run command or ShellExecute, or what?

'CLS' will clear a command console of text... If you are using Stdin and out, simply clear the variable that is printing your text to the GUI, or how ever your doing it...

How are you parsing the CMD text?

Share this post


Link to post
Share on other sites

Its not as complicated as you might think.

Sample of what I would like done.

ConsoleWrite("This is text." )
ConsoleWrite("This is more text." )
Somthing_To_Clear_The_Text()
Exit

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

Its not as complicated as you might think.

Sample of what I would like done.

ConsoleWrite("This is text." )
ConsoleWrite("This is more text." )
Somthing_To_Clear_The_Text()
Exit
You could send the message SCI_CLEARALL to the edit. See here.

Or, you could ControlSend "^a{DEL}" to the class Scintilla2.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

We are getting closer, but this is in the command prompt, not SciTE.

That program, when compiled as a console program will be ran by cmd and interface with the user. I just want it to clear messages that it displayed through cmd.


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

We are getting closer, but this is in the command prompt, not SciTE.

That program, when compiled as a console program will be ran by cmd and interface with the user. I just want it to clear messages that it displayed through cmd.

Ah, well in that case my post was not very helpful.

If it's the command prompt then what was wrong with BinaryBrother's CLS suggestion?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

We are getting closer, but this is in the command prompt, not SciTE.

That program, when compiled as a console program will be ran by cmd and interface with the user. I just want it to clear messages that it displayed through cmd.

Then why won't "CLS" work?

Sorry for my utter ignorance... I guess I'm just not getting something... But as you most know, that's nothing new... :P

Share this post


Link to post
Share on other sites

I am just having the app send information to the user through CMD.

Compile and run this as a console application and you will see what I mean.

ConsoleWrite("This is my program, explaining what it about to do." )
;This is my program doing its thing.
;This is my program doing its thing.
;This is my program doing its thing.
;This is my program doing its thing.
ConsoleWrite("This is my program, explaining the next thing that it is going to do." )
;This is my program doing its thing.
;This is my program doing its thing.oÝ÷ Ù8b²+0«mç§v·¬º[p¢é]$)¶èºÚ®¢×+"²l©®+jg±¦V¢)à­֢ëm¡Ú+"²l©®+jg±¦V¢)ට{m)තج§Ú£ú®¢×z·¬jjøv÷«²*'¡ü!jÒ0¢é])iÇ(b²jëh×6ConsoleWrite("This is my program, explaining what it about to do." )
;This is my program doing its thing.
;This is my program doing its thing.
;This is my program doing its thing.
;This is my program doing its thing.

FUNCTION_TO_CLEAR_SCREEN()

ConsoleWrite("This is my program, explaining the next thing that it is going to do." )
;This is my program doing its thing.
;This is my program doing its thing.

And here is what the end result would look like.

This is my program, explaining the next thing that it is going to do.

In this case, it sayd what it wanted to say, did what it wanted to do, then cleared the screen to say somthing else.

Say this, then do an action.

Clear the screen/

Say somthing else and do an action.

I hope this was a bit more clear than I have been so far.


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

I just found a way to do it!

I had no idea that when you used "_RunDOS()" it will actually output to the command window that runs the command! Thats cool to learn!

To those of you that dont understand, try this:

#include<Process.au3>
ConsoleWrite( "Hi!" & @CRLF)
Sleep( 1500 )
_RunDOS( "cls" )
ConsoleWrite( "How are you?" & @CRLF)
Sleep( 1500 )
_RunDOS( "cls" )
ConsoleWrite( "Ok. Bye." & @CRLF)
Sleep( 1500 )
_RunDOS( "cls" )

Compile that as a console application, and drop it in the sys32 folder. Type the name of the compiled app into cmd, and watch it do what I have been trying to get help with. :unsure:

Well, thanks to BinaryBrother and Martin for trying to help anyway! :P


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

ConsoleWrite() simply sends 'echo' like text, I understand that now... Maybe there should be a ConsoleClear() function... :D

Sorry I wasn't much help, maybe next time! :P

@Martin

I gotta learn to type faster! :unsure: You beat me to bringing up 'CLS'... again... lol

As for Draygoes, I should have figured a guy with 1,131 posts wasn't looking for an answer as easy as CLS... lol

Share this post


Link to post
Share on other sites

I had no idea that when you used "_RunDOS()" it will actually output to the command window that runs the command! Thats cool to learn!

Very interesting... especially when you take a look at the UDF :unsure:

Func _RunDOS($sCommand)
    Local $nResult = RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
    Return SetError(@error, @extended, $nResult)
EndFunc  ;==>_RunDOS

It works, but I've got no idea why the _RunDOS instance of @comspec is attached to the console :P , I would have bet it creates another instance...

Share this post


Link to post
Share on other sites

Very interesting... especially when you take a look at the UDF :unsure:

Func _RunDOS($sCommand)
    Local $nResult = RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
    Return SetError(@error, @extended, $nResult)
EndFunc ;==>_RunDOS

It works, but I've got no idea why the _RunDOS instance of @comspec is attached to the console :P , I would have bet it creates another instance...

Run and RunWait output is redirected to the programs console that runs it by default Windows behavior?

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Hi Draygoes,

I wrote this dll for basic command shell interaction through AutoIt3. http://www.autoitscript.com/forum/index.ph...mp;#entry648013

It supports clearing the console buffer, however the binary size is quite large, so I doubt it is what you are after.

:P

Oops, did not read fully, you are right though, why do with many lines what can be done simply with::

RunWait(@COMSPEC &" /C CLS",@WORKINGDIR,@SW_HIDE)
Edited by Mobius

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

my entry in the 'most code used to duplicate a three letter command' contest

will submit this to Gary after comments

Please don't quote the codebox if replying

Edit1 fixed Local scope $aRet declared in If statement

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

If Not @Compiled Then Exit MsgBox(262144, "_WinAPI_ClearConsole() Test", "Script must be compiled as a console app to run")

Opt('MustDeclareVars', 1)

ConsoleWrite("Jackdaws love my big sphinx of quartz" & @CRLF)
ConsoleWrite("Jackdaws love my big sphinx of quartz" & @CRLF)
ConsoleWrite("Jackdaws love my big sphinx of quartz" & @CRLF)
Sleep(2000)
_WinAPI_ClearConsole()
ConsoleWrite("The quick brown fox jumps over the lazy dog" & @CRLF) 
Sleep(2000)
_WinAPI_ClearConsole()
ConsoleWrite("Paused for 2 seconds" & @CRLF)


;Overwriting StdOut in CUI compiled app, for use with progress indicator
;http://www.autoitscript.com/forum/index.php?showtopic=66486
Local $ClearProgress = 0
Local $processing = "Paused..."
Local $progress = ""

For $i = 1 to 100
    ConsoleWrite(@CR & $processing & $progress)
    Sleep(25)
    $ClearProgress +=1
  $progress &= "."
    If $ClearProgress = 25 then
        ConsoleWrite(@cr & "                                                                     ")
        $ClearProgress = 0
        $progress = ""
    EndiF
Next
ConsoleWrite("")
_WinAPI_ClearConsole()
Sleep(2000)
Exit


; #FUNCTION# =======================================================
; Name...........: _WinAPI_ClearConsole
; Description ...: Clears console screen buffer
; Syntax.........: _WinAPI_WriteConsole()
; Parameters ....: $hConsole    - (Optional) Handle to the console screen buffer
;
; Return values .: Success      - True
;                         Failure        - False
; Author ........: rover
; Modified.......:
; Remarks .......: with code from Screen_Scrape.au3 - Author: Valik
;                        http://www.autoitscript.com/forum/index.php?s=&showtopic=71023&view=findpost&p=527810
; Related .......:   How To Performing [sic] Clear Screen (CLS) in a Console Application
;                        Article ID: 99261 - Last Review: November 21, 2006 - Revision: 3.3
;                        http://support.microsoft.com/kb/99261
;                        Clearing the Screen (Windows) - Win32 & COM (same code as above)
;                        http://msdn.microsoft.com/en-us/library/ms682022.aspx (Build date: 3/12/2009)  
; Link ..........; @@MsdnLink@@ FillConsoleOutputCharacter            
; Example .......; Yes
; ==================================================================
Func _WinAPI_ClearConsole($hConsole = -1)
    
    Local $coordBufferCoord = BitOR(0 * 0x10000, BitAND(0, 0xFFFF)), $bChar = 0x20
    Local Const $STD_OUTPUT_HANDLE = -11
    Local Const $INVALID_HANDLE_VALUE = -1
    Local Const $_CONSOLE_SCREEN_BUFFER_INFO = "short dwSizeX; short dwSizeY;short dwCursorPositionX;" & _
    "short dwCursorPositionY; short wAttributes;short Left; short Top; short Right; short Bottom;" & _
    "short dwMaximumWindowSizeX; short dwMaximumWindowSizeY"

    Local $hDLLK32 = DllOpen("Kernel32.dll"), $aRet
    If $hConsole = -1 Then
        $aRet = DllCall($hDLLK32, "hwnd", "GetStdHandle", "dword", $STD_OUTPUT_HANDLE)
        If @error Then Return SetError(@error, 1, $INVALID_HANDLE_VALUE)
        $hConsole = $aRet[0]
    EndIf
    
    Local $tCONSOLE_SCREEN_BUFFER_INFO = DllStructCreate($_CONSOLE_SCREEN_BUFFER_INFO)
    Local $pConsoleScreenBufferInfo = DllStructGetPtr($tCONSOLE_SCREEN_BUFFER_INFO)
    If @error Then $pConsoleScreenBufferInfo = $tCONSOLE_SCREEN_BUFFER_INFO
    
    $aRet = DllCall($hDLLK32, "int", "GetConsoleScreenBufferInfo", "hwnd", _
    $hConsole, "ptr", $pConsoleScreenBufferInfo)
    If @error Then Return SetError(@error, 2, False)
    
    Local $dwSizeX  = DllStructGetData($tCONSOLE_SCREEN_BUFFER_INFO, "dwSizeX")
    Local $dwSizeY  = DllStructGetData($tCONSOLE_SCREEN_BUFFER_INFO, "dwSizeY")
    Local $dwConSize = $dwSizeX * $dwSizeY
    
    $aRet = DllCall($hDLLK32, "int", "FillConsoleOutputCharacter", "hwnd", $hConsole, _
    "byte", $bChar, "dword", $dwConSize, "dword", $coordBufferCoord, "int*", 0)
    If @error Or UBound($aRet) <> 6 Or $aRet[5] <> $dwConSize Then Return SetError(@error, 3, False)

    $aRet = DllCall($hDLLK32, "int", "GetConsoleScreenBufferInfo", "hwnd", _
    $hConsole, "dword", $pConsoleScreenBufferInfo)
    If @error Then Return SetError(@error, 4, False)
    Local $wAttribute  = DllStructGetData($tCONSOLE_SCREEN_BUFFER_INFO, "wAttributes")

    $aRet = DllCall($hDLLK32, "int", "FillConsoleOutputAttribute", "hwnd", $hConsole, _
    "short", $wAttribute, "dword", $dwConSize,  "dword", $coordBufferCoord, "int*", 0)
    If @error Or UBound($aRet) <> 6 Or $aRet[5] <> $dwConSize Then Return SetError(@error, 5, False)

    $aRet = DllCall($hDLLK32, "int", "SetConsoleCursorPosition", "hwnd", _
    $hConsole, "dword", $coordBufferCoord)
    If @error Then Return SetError(@error, 6, False)
    DllClose($hDLLK32)
    Return SetError(@error, 0, True)
EndFunc
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

All hail Rover, king of the TLR :P

(TLR - three letter replacement)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

..........Say this, then do an action.

Clear the screen/

Say somthing else and do an action.

I hope this was a bit more clear than I have been so far.

Hello

hope my question will not seems too stupid, but why not using Progresson / progressset ? : you will have a nice progress bar on the screen, and you can add text above and under the progress bar...

Share this post


Link to post
Share on other sites

Hello

hope my question will not seems too stupid, but why not using Progresson / progressset ? : you will have a nice progress bar on the screen, and you can add text above and under the progress bar...

Hi sksbir,

Your question does not seem stupid in any sense of the word.

Yes it does make sense if you look at it from a standpoint of a GUI based program, but I am making a console based program, completely void of any controls. But your point is valid.


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Well, I was origonally searching for a function that would work, but I found a workaround. However, this does not mean that my search should be a waste right?

Thus, I present a simple UDF :unsure:

Func ConsoleClear()
    Local $nResult = RunWait(@ComSpec & " /C  CLS" , "", @SW_HIDE)
    Return SetError(@error, @extended, $nResult)
EndFuncoÝ÷ ØEÀ0òÄjëh×6ConsoleWrite( "Hello")
Sleep(2000)
ConsoleClear()
ConsoleWrite("Goodbye")
ConsoleClear()
Exit




Func ConsoleClear()
    Local $nResult = RunWait(@ComSpec & " /C  CLS" , "", @SW_HIDE)
    Return SetError(@error, @extended, $nResult)
EndFunc

Hey, why not right? :P

Edited by Draygoes

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0