Jump to content
Sign in to follow this  
nazirm

command prompt and StdinWrite

Recommended Posts

nazirm

Hi:

I wanted to study and understand StdinWrite function and so wrote the following two pieces of code.  The first with StdinWrite works, the second does not.  Why?  For the second code I only see the C:> prompt and nothing else happens.

Regards,

Nazir

#include <Constants.au3>
$pid = Run("C:\Windows\system32\cmd.exe","C:\", $STDIN_CHILD + $STDOUT_CHILD)
WinWaitActive("C:\Windows\system32\cmd.exe")
send("cd ")
send("C:\users\test1")
send("{enter}")
Sleep(10000)
Exit
#include <Constants.au3>
$pid = Run("C:\Windows\system32\cmd.exe","C:\", $STDIN_CHILD + $STDOUT_CHILD)
WinWaitActive("C:\Windows\system32\cmd.exe")
StdinWrite($pid,"cd")
StdinWrite($pid,"C:\users\test1")
StdinWrite($pid,@CRLF)
Sleep(10000)
Exit
Edited by nazirm

Share this post


Link to post
Share on other sites
water

You are missing a parameter. Try:

$pid = Run("C:\Windows\system32\cmd.exe", @SystemDir, @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)

BTW: Why did you open two threads with the same subject?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
nazirm

Hi Water.  Thanks I tried what you said now I get completely blank scree.  Some one please help. 

Sorry...I inadvertently created another thread...how do I delete it?

Share this post


Link to post
Share on other sites
water

Thats because you grab StdOut by using "$STDOUT_CHILD".

Remove it or grab the output and do whatever you need to do.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
nazirm

Thanks again Water.  Now I understand better about stdin and stdout.  As suggested I am grabbing the output and reading the data.  Now I want to write data to the prompt, read the output, write th data again and then read the output again.  Here is the code I have written.  The first MsgBox display is correct but the second is blank! Why?

#include <Constants.au3>
$pid = Run("C:\Windows\system32\cmd.exe",@SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    StdinWrite($pid,"cd ")
    StdinWrite($pid,"C:\users\test1")
    StdinWrite($pid,@CRLF)
    StdinWrite($pid)
    Local $data
    While True
        $data &= StdoutRead($pid)
        If @error Then
            MsgBox(0,"","error")
            ExitLoop
        EndIf

    WEnd
    MsgBox(0, "Debug", $data); first message box
    StdinWrite($pid,"cd ")
    StdinWrite($pid,"C:\users\test2")
    StdinWrite($pid,@CRLF)
    StdinWrite($pid)
    $data = ""
    While True
        $data &= StdoutRead($pid)
        If @error Then
            MsgBox(0,"","error")
            ExitLoop
        EndIf

    WEnd
    MsgBox(0, "Debug", $data); second message box 
Sleep(10000)
Exit

Share this post


Link to post
Share on other sites
water

Can you please tell us what you try to achieve?

I think there should be an easier way to do what you want to do.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
nazirm

I have a program, perl script, which requires me to enter data while it is running and when it requests.  I want to automate this process.  Let me put it in steps:

1.  Run from command prompt:  perl xxxx.pl param1=aaaa param2=bbbbb

2.  Capture the output on the screen on to a variable.

3.  When it asks for admin password enter the password.

4.  Capture the data it prints on the screen to a variable

5.  When it asks if we should archive the results.  Enter "yes" or "no".  Autoit script will have the logic to determine yes or no.

6.  Capture the data it prints on the screen to a variable

I want to automate the above steps using autoit.  Can you help me?

Share this post


Link to post
Share on other sites
water

Maybe you need to add a "Sleep(2000" (two seconds as ane xample) after your last StdInWrite to let the program process your input and create the new output.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
MHz

I have an example to show. :idea:

This is a CMD script to test with. Save it as test.cmd in the same directory as the AutoIt script.

@echo off
echo Please enter the password
set /p password=
echo Do you want to archive the results
set /p archive=
echo password = %password%
echo archive = %archive%
echo Thankyou and have a nice day

And here is the AutoIt script with comments. Put in same directory as test.cmd. You can name this AutoIt script what-ever-you-like.au3. ^_^

Global $data
#include <Constants.au3>
$pid = Run("cmd /c test.cmd", @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
While ProcessExists($pid)
    ; test @extended here as StdoutRead does not set @error due to StdInWrite keeping the stream open
    Do
        Sleep(10)
        $data &= StdoutRead($pid)
    Until Not @extended Or @error
    ; check for the prompts and answer with StdInWrite
    If StringInStr($data, 'Please enter the password') Then
        MsgBox(0, "Debug", 'StdOut: ' & $data & @CRLF & 'StdIn: strawberry')
        StdInWrite($pid, 'strawberry')
        ; clear $data so the next prompt can be new without concern of old data
        $data = ''
    ElseIf StringInStr($data, 'Do you want to archive the results') Then
        MsgBox(0, "Debug", 'StdOut: ' & $data & @CRLF & 'StdIn: yes')
        StdInWrite($pid, 'yes')
        ; close StdinWrite
        StdinWrite($pid)
        ; clear $data and get final $data message
        $data = ''
        ; test @error now as only StdoutRead is keeping the stream open
        Do
            Sleep(10)
            $data &= StdoutRead($pid)
        Until @error
        ; done, get out of the loop
        ExitLoop
    EndIf
WEnd
; show final message
MsgBox(0, "Debug", 'StdOut: ' & $data)
If ProcessExists($pid) Then Exit 1

Exit

Run the AutoIt script.

And now some output.

---------------------------
Debug
---------------------------
StdOut: Please enter the password



StdIn: strawberry
---------------------------
OK   
---------------------------
---------------------------
Debug
---------------------------
StdOut: Do you want to archive the results



StdIn: yes
---------------------------
OK   
---------------------------
---------------------------
Debug
---------------------------
StdOut: password = strawberry

archive = yes

Thankyou and have a nice day


---------------------------
OK   
---------------------------

And that is it. The last message box seems to have the right answers. :)

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  

×