Sign in to follow this  
Followers 0
Angel

ControlSend to a command line window

13 posts in this topic

Hi,

is there a way to use ControlSend to send keystrokes to a command line window (cmd.exe)?

We have a script that uses Send() to send some commands to a command line window (actually it is a bash console that we open using cygwin). The problem is that if someome uses the PC while the script (which takes a long time to finish its work) is running, it breaks the script flow. We make sure to use WinActivate() before everycall to Send(), but the commands that we send are very long and still sometimes people will interrupt the Send() when it is half way through sending one command.

I have reduced the Keystroke time to 1 ms (I am a bit wary of reducing it further, to 0), but there are still some problems.

My current solution is to use RunWait() to run each command separatelly. However the person using our program does not like this because he wants to have the whole history of commands (and results) in the command line when the script finishes.

What would be great is to be able to use ControlSend() instead of Send(), but it does not work with a Command line window, because it is not an edit control. So I wonder if there is some other clever way of doing this while using a single command line.

Thanks!

Angel

Share this post


Link to post
Share on other sites



Check out the STDIO functions in the latest beta. I have not tested this out myself but I believe you can do what you want that way.

Another way would be to use runwait as you are now and create a fake console window to keep a history of what is being done and show any errors.

Share this post


Link to post
Share on other sites

I don't know how "clever" this is, but you may be surprised it works. ControlSend does not in fact require a control ID and can send to a window:

ControlSend("C:\WINDOWS\system32\cmd.exe", "", "", "Your command sting here" & @CR)

Since each CMD window gets created with the same title, I'd recommend getting a handle for the window immediately and then creating a new CMD window will not screw you up:

$hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe")
ControlSend($hCmd, "", "", "Your command sting here" & @CR)

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

I don't know how "clever" this is, but you may be surprised it works. ControlSend does not in fact require a control ID and can send to a window:

ControlSend("C:\WINDOWS\system32\cmd.exe", "", "", "Your command sting here" & @CR)

Since each CMD window gets created with the same title, I'd recommend getting a handle for the window immediately and then creating a new CMD window will not screw you up:

$hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe")
ControlSend($hCmd, "", "", "Your command sting here" & @CR)

Dale

Hi Dale, thanks for the suggestion, as it would do exactly what I want, but I cannot make it work in my machine. Have you tried it on your PC?

Angel

Share this post


Link to post
Share on other sites

Hi Dale, thanks for the suggestion, as it would do exactly what I want, but I cannot make it work in my machine. Have you tried it on your PC?

Angel

Actually I made it work. The title of my CMD window has System32 with capital S. This is great! Is this a proper feature or is it abusing the function? If it is a feature, and I hope it is, it should be added as an example to the help file! This is great! Thanks A LOT! :o You the man! B)

Angle

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

this worked

Run(@ComSpec & " /k " & 'dir c:\', "", @SW_SHOW)
Sleep(3000)

$hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe")
ControlSend($hCmd, "", "", "dir c:\" & @CR)

Sleep(3000)
ControlSend($hCmd, "", "", "exit" & @CR)

8)

well... better late then never...lol

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Actually, it does not really work perfectly, or perhaps I've found a bug.

On Windows 2000, if I do:

ControlSend("C:\WINDOWS\system32\cmd.exe", "", "", "u:{enter}")

Instead of seeing "u:" being typed I actually see "u;" (that is, I get a semicolon instead of a colon!)!. I've tried every combination that I could think of, I've even tried to set the optional parameter to 1 to send the raw keys but I still send a semicolon instead of a colon (and I can clearly see the colon in the text editor).

Can anyone confirm this please? How do I report this bug?

Thanks!

Angel

Share this post


Link to post
Share on other sites

Actually, it does not really work perfectly, or perhaps I've found a bug.

On Windows 2000, if I do:

ControlSend("C:\WINDOWS\system32\cmd.exe", "", "", "u:{enter}")

Instead of seeing "u:" being typed I actually see "u;" (that is, I get a semicolon instead of a colon!)!. I've tried every combination that I could think of, I've even tried to set the optional parameter to 1 to send the raw keys but I still send a semicolon instead of a colon (and I can clearly see the colon in the text editor).

Can anyone confirm this please? How do I report this bug?

Thanks!

Angel

Ah, so I'm guessing that your example demonstrates why this remark is in the ControlSend help text:

ControlSend is only unreliable for command prompts as that works differently to normal windows (seems to check physical states rather than accepting the keystroke messages). For normal windows ControlSend should be way more reliable than a normal Send - and yes it does send shift, ctrl, alt etc.

I didn't understand it when I read it first, but care must obviously be taken with the CMD windows - YMMV.

You report bugs in the "v3 Bug Reports" forum. You'll want to determine whether the information above covers it before you post the report however.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Ah, so I'm guessing that your example demonstrates why this remark is in the ControlSend help text:

I didn't understand it when I read it first, but care must obviously be taken with the CMD windows - YMMV.

You report bugs in the "v3 Bug Reports" forum. You'll want to determine whether the information above covers it before you post the report however.

Dale

Umm, I cannot see this remark in my ControlSend help file. What version of AutoIt have you installed? I don't really understand what it means that "seems to check physical states rather than accepting the keystroke messages"...

I'll see what they say in the Bugs forum. Thanks!

Angel

Share this post


Link to post
Share on other sites

Umm, I cannot see this remark in my ControlSend help file. What version of AutoIt have you installed? I don't really understand what it means that "seems to check physical states rather than accepting the keystroke messages"...

I'll see what they say in the Bugs forum. Thanks!

Angel

I'm running the latest beta... Remember that the CMD window is an interface to DOS and not Windows. By "checking the physical state" I presume that they mean it actually polls the keyboard for the state of the modifier keys instead of checking a software status bit.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I have found a free, enhanced DOS command window replacement called 4Dos that works correctly with shifted keys. You'll find it here (scroll to the bottom of the page). 4Dos is highlighted on the 2005 Pricelessware list.

Dale

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

If you open a created au3 script in your Scite4AutoIt3 directory and then go to the output pane and type cmd or help on a empty line, what do you see?

Edit: I am not sure if it is useful information, but I thought I would mention it.

CODE
>help

For more information on a specific command, type HELP command-name

ASSOC Displays or modifies file extension associations.

AT Schedules commands and programs to run on a computer.

ATTRIB Displays or changes file attributes.

BREAK Sets or clears extended CTRL+C checking.

CACLS Displays or modifies access control lists (ACLs) of files.

CALL Calls one batch program from another.

CD Displays the name of or changes the current directory.

CHCP Displays or sets the active code page number.

CHDIR Displays the name of or changes the current directory.

CHKDSK Checks a disk and displays a status report.

CHKNTFS Displays or modifies the checking of disk at boot time.

CLS Clears the screen.

CMD Starts a new instance of the Windows command interpreter.

COLOR Sets the default console foreground and background colors.

COMP Compares the contents of two files or sets of files.

COMPACT Displays or alters the compression of files on NTFS partitions.

CONVERT Converts FAT volumes to NTFS. You cannot convert the

current drive.

COPY Copies one or more files to another location.

DATE Displays or sets the date.

DEL Deletes one or more files.

DIR Displays a list of files and subdirectories in a directory.

DISKCOMP Compares the contents of two floppy disks.

DISKCOPY Copies the contents of one floppy disk to another.

DOSKEY Edits command lines, recalls Windows commands, and creates macros.

ECHO Displays messages, or turns command echoing on or off.

ENDLOCAL Ends localization of environment changes in a batch file.

ERASE Deletes one or more files.

EXIT Quits the CMD.EXE program (command interpreter).

FC Compares two files or sets of files, and displays the differences

between them.

FIND Searches for a text string in a file or files.

FINDSTR Searches for strings in files.

FOR Runs a specified command for each file in a set of files.

FORMAT Formats a disk for use with Windows.

FTYPE Displays or modifies file types used in file extension associations.

GOTO Directs the Windows command interpreter to a labeled line in a

batch program.

GRAFTABL Enables Windows to display an extended character set in graphics

mode.

HELP Provides Help information for Windows commands.

IF Performs conditional processing in batch programs.

LABEL Creates, changes, or deletes the volume label of a disk.

MD Creates a directory.

MKDIR Creates a directory.

MODE Configures a system device.

MORE Displays output one screen at a time.

MOVE Moves one or more files from one directory to another directory.

PATH Displays or sets a search path for executable files.

PAUSE Suspends processing of a batch file and displays a message.

POPD Restores the previous value of the current directory saved by PUSHD.

PRINT Prints a text file.

PROMPT Changes the Windows command prompt.

PUSHD Saves the current directory then changes it.

RD Removes a directory.

RECOVER Recovers readable information from a bad or defective disk.

REM Records comments (remarks) in batch files or CONFIG.SYS.

REN Renames a file or files.

RENAME Renames a file or files.

REPLACE Replaces files.

RMDIR Removes a directory.

SET Displays, sets, or removes Windows environment variables.

SETLOCAL Begins localization of environment changes in a batch file.

SHIFT Shifts the position of replaceable parameters in batch files.

SORT Sorts input.

START Starts a separate window to run a specified program or command.

SUBST Associates a path with a drive letter.

TIME Displays or sets the system time.

TITLE Sets the window title for a CMD.EXE session.

TREE Graphically displays the directory structure of a drive or path.

TYPE Displays the contents of a text file.

VER Displays the Windows version.

VERIFY Tells Windows whether to verify that your files are written

correctly to a disk.

VOL Displays a disk volume label and serial number.

XCOPY Copies files and directory trees.

>Exit code: 1 Time: 0.315

Edited by MHz

Share this post


Link to post
Share on other sites

Instead of seeing "u:" being typed I actually see "u;" (that is, I get a semicolon instead of a colon!)!.

I posted this little script in another thread about this same issue:

AutoItSetOption("WinTitleMatchMode", 2)
Run("cmd")
WinWaitActive("cmd")

For $i = 30 To 132
    WinActivate("cmd")
    WinWaitActive("cmd")
    Send("{BS 40}")
    Send("Chr " & $i & "...Send...>" & Chr($i))
    ControlSend("cmd", "", "", Chr($i))
    Send("<...ControlSend")
    
    MsgBox(0, "Chr(" & $i & ")", Chr($i), 2)
Next
It lets you see the difference between send and controlsend side by side.

The other thread ended at w0uter's FTP code:

http://www.autoitscript.com/forum/index.php?showtopic=12473

Perhaps something in there might be of some help.


[size="1"][font="Arial"].[u].[/u][/font][/size]

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