Bone09

Sending keystrokes in CMD

13 posts in this topic

#1 ·  Posted (edited)

Hello everyone,
 
I created a script that sends a series of commands on CMD through the use of 'ControlSend()' function together with 'Sleep()' after every command to ensure that the commands were sent. But when my computer somewhat slowed down, some commands were skipped out.
 
I wonder if there is a function (other than increasing the value of sleep) that waits or detects when it is already time to type in the CMD. Or maybe if there is a function that waits for the 'i-beam' to inform the program that it is already the time to send the commands.
  

Thanks!
 
; I created a function that sends the commands directly to the cmd regardless whether the window is active or not

Func Command($text)
$hWnd = WinWait("[CLASS:ConsoleWindowClass]", "", 10)
Sleep(500)
ControlSend($hWnd, "", "", $text)
EndFunc
; This is the main codeRun("cmd.exe")
Command("This is my first command")
Sleep(500) ;for the delay
Command("This is my second command")
Sleep(50) ;delay again
.
.
.
Edited by bonsantiago

Share this post


Link to post
Share on other sites



Most of the time you use StdinWrite and StdoutRead to communicate with CMD.

How do you start CMD?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Most of the time you use StdinWrite and StdoutRead to communicate with CMD.

How do you start CMD?

 

I start cmd through Run(cmd.exe)

Then followed by the series of commands that I want to send.

Share this post


Link to post
Share on other sites

Or just do a Run()...no need to interact with a console.

 

I have a series of commands, so i need to interact with the console.

Share this post


Link to post
Share on other sites

what are the commands?

Share this post


Link to post
Share on other sites

what are the commands?

 

For example,

Send(GUICtrlRead($UsernameInput) & "{ENTER}")
Sleep(500)
Send(GUICtrlRead($PasswordInput) & "{ENTER}")
Sleep(500)
Send("cd /files0/ & "{ENTER}")

The Send + Sleep function is not stable and this is why I'm looking for other ways of sending keys in the putty console.

Share this post


Link to post
Share on other sites

I start cmd through Run(cmd.exe)

Then followed by the series of commands that I want to send.

Then please have a look at StdinWrite. The example shows how to send commands.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Another question. Are you sure the commands you want to run can't be translated to pure AutoIt?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Then please have a look at StdinWrite. The example shows how to send commands.

 

Hi, thank you for the response but I cannot use StdinWrite as this function constantly needs a directory path.

I'm actually running cmd for the purpose of executing a 3rd party application (for example a java app) through console (access the directory of the 3rd party app ONCE) and run the jar file along with the series of commands

Edited by Bone09

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I cannot use StdinWrite as this function constantly needs a directory path.

 

Can you clarify this statement? What do you mean by a function constantly needing a directory path? There's bound to be better ways to approach this. Also, I still don't see how you need to manually steer cmd.exe to access a directory through keypresses and start a java application?

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Can you clarify this statement? What do you mean by a function constantly needing a directory path? There's bound to be better ways to approach this. Also, I still don't see how you need to manually steer cmd.exe to access a directory through keypresses and start a java application?

 

The java application is not gui-based. I need to use the console to execute the commands to communicate with the program

Share this post


Link to post
Share on other sites

That is exactly what the STDxxx functions are being made for. You can send commands to the application, read the displayed data, then send another command etc.

Just give them a try.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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