Jump to content

Execute CMD with multiple line


gahhon
 Share

Recommended Posts

I have a python script automate.py and I wanna run it via CMD.
As I know to execute python via CMD is something like this

$CMD = "Something python here" 
Run(@ComSpec & " /c " & $CMD)

But how can I just open the 1 CMD and send multiple commands to the CMD based on the function called?

For instance like:

image.png.23596f93e1f674c341f3d318eb950964.png

  1. Launch CMD and send python then send import automate
  2. If google() function is called - send automate.google() to the CMD
  3. If yahoo() function is called - send automate.yahoo() to the CMD

 I don't want to execute multiple CMD to call the automate functions. This is due to inside the python script have global variable to handle duplicate flag.
So if I execute multiple CMD to call the functions, the global variable will be keep new as NULL. Which is not my expectation.

 

Thanks

Link to comment
Share on other sites

Link to comment
Share on other sites

1 hour ago, Nine said:

Not specifically to python, but the more general approach with StdinWrite, StdOutRead, Run (), you should find tons of examples

Anyway, I cannot work with the method of StdinWrite, StdOutRead and Run(). It just open empty CMD without trigger anything tho.

But somehow I found this method send hidden command text and it work perfectly as expect as well as found some source how to hide CMD window with WinWait.

Finally my sample code:

Global $handle, $iFlag = False

Func CMDSetup()
    Run(@ComSpec & " /k TITLE HideCmd","",@SW_HIDE)
    $handle = (WinWait("HideCmd", "", 3) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : SetError(1, 0, 0))
    ControlSend($handle, "", "", "python" & @CRLF)
    ControlSend($handle, "", "", "import automate;" & @CRLF)
    $iFlag = True
EndFunc

Func Google()
    If $iFlag = False Then CMDSetup()
    ControlSend($handle, "", "", "automate.google()" & @CRLF)
EndFunc

Func Yahoo()
    If $iFlag = False Then CMDSetup()
    ControlSend($handle, "", "", "automate.yahoo()" & @CRLF)
EndFunc

Thanks.

Edited by gahhon
Successful to achieve result
Link to comment
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
 Share

×
×
  • Create New...