weszzer

Run(@ComSpec) and instead of Send Command

15 posts in this topic

Hi Guys,

My code below is working fine. I wondering if I can use the Run (@comspec ..command) using my extract.exe file.

Run("cmd.exe")
Sleep(2000)
Send("C:\directory\extract.exe -DATE " & @MDAY & "/" & @MON & "/" & @YEAR & "/" & @HOUR)

The code below return 0 error when I compile it. but when I run nothing happened, the cmd-window didn't show up

Run(@ComSpec & '/k "extract.exe -I' & @YEAR &"/" & @MON & "/" & @MDAY-1, @SW_SHOW)

My intention is to run the Autoit-compiled exe file, then trigger the command-code..

Please help..

Thank you.

Share this post


Link to post
Share on other sites



Insert a space before "/k".

Run(@ComSpec & ' /k "extract.exe -I' & @YEAR &"/" & @MON & "/" & @MDAY-1, @SW_SHOW)
1 person likes this

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

#3 ·  Posted (edited)

1. Do you have to run the exe through cmd.exe?

2. There's no space separating your switch /k and @Comspec and no space after -I

3. Your quotes are off, look at the "single" double quote you have in the string from '/k to -I'.

4. You have @SW_SHOW, in the working directory parameter 

5. @MDay - 1 is going to cause you trouble at the first day of the month, best to use _DateAdd().

So how I'd run the last part of the code.

#include <Date.au3>
 
Global $gsDayAdd = _DateAdd("D", -1, @YEAR & "/" & @MON & "/" & @MDAY)
Run(@ComSpec & ' /k "extract.exe" -I ' & $gsDayAdd, "", @SW_SHOW)

 

Edit:

If you're having issues with Run, quotes, understanding parameters, this may help a little.

It's a custom little run function.

1st parameter is the exe you're going to run (eg. @Comspec)

2nd parameter to the 11th parameter are your command line switches you want to run (if you need more than 10, just follow the code layout for the function)

It makes sure it takes care of the double quotes and spaces necessary for you to run good code.

#include <Date.au3>

Global $gsDayAdd = _DateAdd("D", -1, _NowCalcDate())
Global $gsCommandString = _RunBuildCommandString(@ComSpec, "/k", "extract.exe", "-I", $gsDayAdd)
ConsoleWrite($gsCommandString & @CRLF)

;Run($gsCommandString, "", @SW_SHOW)

Func _RunBuildCommandString($sExe, $vparam1="",$vparam2="",$vparam3="", _
        $vparam4="",$vparam5="", $vparam6="",$vparam7="",$vparam8="", _
        $vparam9="",$vparam10=""); add more params if you need more

    ; check for spaces in exe string
    If StringRegExp($sExe, "^\s*[^\x22].*?\h") Then
        $sExe = '"' & $sExe & '"'
    EndIf

    Local $sCommandString, $sVal
    For $i = 1 To @NumParams - 1
        $sVal = Eval("vparam" & $i)
        $sCommandString &= (StringRegExp($sVal, "^\s*[^\x22].*?\h") ? _
            ' "' & $sVal & '"' : " " & $sVal)
    Next

    $sCommandString = $sExe & $sCommandString

    Return $sCommandString
EndFunc

Good luck

Edited by SmOke_N
2 people like this

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Hi SmOke_N,

I added the directory of my extract.exe also I added the Send ("{enter}") to execute the application

Global $gsDayAdd = _DateAdd("D", -1, _NowCalcDate())
Global $gsCommandString = _RunBuildCommandString(@ComSpec, "/k", "C:\directory\extract.exe", "-I", $gsDayAdd)
ConsoleWrite($gsCommandString & @CRLF)
Sleep(1000)
Send("{Enter}")







 

And I got this results:

"C:\Windows\System32\cmd.exe /k C:directoryextract.exe -I 2015/01/12"

Which is correct.

1. Do you have to run the exe through cmd.exe?

Yes, the extract.exe is 3rdParty tool there are cmd parameters associated to the extract.exe and it's only run on cmd console/window.

Share this post


Link to post
Share on other sites

Good, you can uncomment the Run() command, and use the code as if the the ConsoleWrite() function wasn't there.

Here you go, a full solution, that will even help with your hidden windows, the _getCMDWin() func will wait for the command console window to appear for that process id before moving forward, then you can use the ControlSend() funcs without the need of the command console always having to be active or have focus.

#include <Date.au3>

Global $gsDayAdd = _DateAdd("D", -1, _NowCalcDate())

Global $gsCommandString = _RunBuildCommandString(@ComSpec, "/k", "extract.exe", "-I", $gsDayAdd)

Global $giPID = Run($gsCommandString, "", @SW_SHOW)

Global $ghCMDWnd = _getCMDWin($giPID)
If @error Then
    ConsoleWrite("Error: " & @error & @CRLF)
    Exit 101
EndIf

; no need to sleep if you don't want to, you wouldn't be here if the window didn't exist
ControlSend($ghCMDWnd, "", "", "{ENTER}")

Func _RunBuildCommandString($sExe, $vparam1="",$vparam2="",$vparam3="", _
        $vparam4="",$vparam5="", $vparam6="",$vparam7="",$vparam8="", _
        $vparam9="",$vparam10=""); add more params if you need more

    ; check for spaces in exe string
    If StringRegExp($sExe, "^\s*[^\x22].*?\h") Then
        $sExe = '"' & $sExe & '"'
    EndIf

    Local $sCommandString, $sVal
    For $i = 1 To @NumParams - 1
        $sVal = Eval("vparam" & $i)
        $sCommandString &= (StringRegExp($sVal, "^\s*[^\x22].*?\h") ? _
            ' "' & $sVal & '"' : " " & $sVal)
    Next

    $sCommandString = $sExe & $sCommandString

    Return $sCommandString
EndFunc

Func _getCMDWin($iPID, $fWait = 1)

    $iPID = ProcessExists($iPID)
    If Not $iPID Then
        Return SetError(1, 0, 0)
    EndIf

    Local $aWList
    Local $iTimer = TimerInit() ; 5 minute timeout

    While 1
        $aWList = WinList("[REGEXPTITLE:(?i).*?\bcmd\b\.exe$]")

        If IsArray($aWList) Then
            For $i = 1 To UBound($aWList) - 1
                If WinGetProcess($aWList[$i][1]) = $iPID Then
                    Return $aWList[$i][1]
                EndIf
            Next
        EndIf

        If ((TimerDiff($iTimer) / 1000) >= 300) Then ExitLoop
        Sleep(10)
    WEnd

    Return SetError(3, 0, 0)
EndFunc

Be sure to share if you see others in need ;) .

1 person likes this

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Good, you can uncomment the Run() command, and use the code as if the the ConsoleWrite() function wasn't there.

Here you go, a full solution, that will even help with your hidden windows, the _getCMDWin() func will wait for the command console window to appear for that process id before moving forward, then you can use the ControlSend() funcs without the need of the command console always having to be active or have focus.

#include <Date.au3>

Global $gsDayAdd = _DateAdd("D", -1, _NowCalcDate())

Global $gsCommandString = _RunBuildCommandString(@ComSpec, "/k", "extract.exe", "-I", $gsDayAdd)

Global $giPID = Run($gsCommandString, "", @SW_SHOW)

Global $ghCMDWnd = _getCMDWin($giPID)
If @error Then
    ConsoleWrite("Error: " & @error & @CRLF)
    Exit 101
EndIf

; no need to sleep if you don't want to, you wouldn't be here if the window didn't exist
ControlSend($ghCMDWnd, "", "", "{ENTER}")

Func _RunBuildCommandString($sExe, $vparam1="",$vparam2="",$vparam3="", _
        $vparam4="",$vparam5="", $vparam6="",$vparam7="",$vparam8="", _
        $vparam9="",$vparam10=""); add more params if you need more

    ; check for spaces in exe string
    If StringRegExp($sExe, "^\s*[^\x22].*?\h") Then
        $sExe = '"' & $sExe & '"'
    EndIf

    Local $sCommandString, $sVal
    For $i = 1 To @NumParams - 1
        $sVal = Eval("vparam" & $i)
        $sCommandString &= (StringRegExp($sVal, "^\s*[^\x22].*?\h") ? _
            ' "' & $sVal & '"' : " " & $sVal)
    Next

    $sCommandString = $sExe & $sCommandString

    Return $sCommandString
EndFunc

Func _getCMDWin($iPID, $fWait = 1)

    $iPID = ProcessExists($iPID)
    If Not $iPID Then
        Return SetError(1, 0, 0)
    EndIf

    Local $aWList
    Local $iTimer = TimerInit() ; 5 minute timeout

    While 1
        $aWList = WinList("[REGEXPTITLE:(?i).*?\bcmd\b\.exe$]")

        If IsArray($aWList) Then
            For $i = 1 To UBound($aWList) - 1
                If WinGetProcess($aWList[$i][1]) = $iPID Then
                    Return $aWList[$i][1]
                EndIf
            Next
        EndIf

        If ((TimerDiff($iTimer) / 1000) >= 300) Then ExitLoop
        Sleep(10)
    WEnd

    Return SetError(3, 0, 0)
EndFunc

Be sure to share if you see others in need ;) .

 

I just came back form overseas trip. Many thanks for the help. I'll check this again.

Yes, I will share and help others.. no problems.

Share this post


Link to post
Share on other sites

The code is quite complicated to me :-) ..

I'm still checking and testing on how and where to insert my code

Now i'm stuck on this error.

No variable given for "Dim", "Local", "Global", "Struct" or "Const" statement

unc _RunBuildCommandString($sExe, $vparam1="",$vparam2="",$vparam3="", $vparam4="",$vparam5="", $vparam6="",$vparam7="",$vparam8="", $vparam9="",$vparam10="")
Func _RunBuildCommandString($sExe, $vparam1="",$vparam2="",$vparam3="", $vparam4="",$vparam5="", $vparam6="",$vparam7="",$vparam8="", $vparam9="",$vparam10="")^ ERROR

Please help..

Thank you

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

$sExe = Full path to executable file to run

$vparam[1-10] =  the command line (switches if you will) that you want to run

@Comspec = Full path to exe

"/k" = command line 1

"extract.exe" = command line 2

"-I" = command line 3

$gsDayAdd = command line 4

You can have up to 10 command lines with that one function.

It returns a properly formatted Run() string for the first parameter of Run().

Edit:

Run returns the process identification number (PID) of the executable you ran with it.

The _getCMD() function, uses that PID to find your cmd.exe window.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

$sExe = Full path to executable file to run

$vparam[1-10] =  the command line (switches if you will) that you want to run

@Comspec = Full path to exe

"/k" = command line 1

"extract.exe" = command line 2

"-I" = command line 3

$gsDayAdd = command line 4

You can have up to 10 command lines with that one function.

It returns a properly formatted Run() string for the first parameter of Run().

Edit:

Run returns the process identification number (PID) of the executable you ran with it.

The _getCMD() function, uses that PID to find your cmd.exe window.

 

Hi SmOke_N,

Here's the complete code, it works as per the code you've given- thanks for your effort.

I have question on how to make a message when the extraction is complete. I added a code to check the process (PID) if the extract.exe if not exist then it will pop-up a message that extract complete.

I don't know where to insert this code I tried to put anywhere but it didn't work

#include <date.au3>
@YEAR & @MON & @MDAY - 1 & ".csv -D " & @YEAR & "/" & @MON & "/" & @MDAY - 1)
Global $gsDayAdd = _DateAdd("D", -1, _NowCalcDate())
Global $gsCommandString = _RunBuildCommandString(@ComSpec, "/k", "extract.exe", "-I", $gsDayAdd)
Global $giPID = Run($gsCommandString, "", @SW_SHOW)
Global $ghCMDWnd = _getCMDWin($giPID)

If @error Then
    ConsoleWrite("Error: " & @error & @CRLF)
    Exit 101
EndIf

; no need to sleep if you don't want to, you wouldn't be here if the window didn't exist
ControlSend($ghCMDWnd, "", "", "{ENTER}")

Func _RunBuildCommandString($sExe, $vparam1="",$vparam2="",$vparam3="", _
        $vparam4="",$vparam5="", $vparam6="",$vparam7="",$vparam8="", _
        $vparam9="",$vparam10=""); add more params if you need more

    ; check for spaces in exe string
    If StringRegExp($sExe, "^\s*[^\x22].*?\h") Then
        $sExe = '"' & $sExe & '"'
    EndIf

    Local $sCommandString, $sVal
    For $i = 1 To @NumParams - 1
        $sVal = Eval("vparam" & $i)
        $sCommandString &= (StringRegExp($sVal, "^\s*[^\x22].*?\h") ? _
            ' "' & $sVal & '"' : " " & $sVal)
    Next

    $sCommandString = $sExe & $sCommandString

    Return $sCommandString
EndFunc

Func _getCMDWin($iPID, $fWait = 1)

    $iPID = ProcessExists($iPID)
    If Not $iPID Then
        Return SetError(1, 0, 0)
    EndIf

    Local $aWList
    Local $iTimer = TimerInit() ; 5 minute timeout
   While 1
      $aWList = WinList("[REGEXPTITLE:(?i).*?\bcmd\b\.exe$]")

        If IsArray($aWList) Then
            For $i = 1 To UBound($aWList) - 1
                If WinGetProcess($aWList[$i][1]) = $iPID Then
                    Return $aWList[$i][1]
                EndIf
            Next
        EndIf
        If ((TimerDiff($iTimer) / 1000) >= 300) Then ExitLoop
        Sleep(10)


    WEnd

   Return SetError(3, 0, 0)
EndFunc

My code to check if the exe file is exist:

$PID_Check=ProcessWaitClose("extract.exe"); check if exe is in pid process
if $PID_Check = 1 Then ;1=not exist
ProcessClose("cmd.exe"); close cmd window
Sleep(2000)
MsgBox(0, "", "file extracted")

Thank you.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

For some reason, the code didn't work. The extract.exe was not executed. It is not available on task manager during the process

I trying to figured out what happened.

Need your help please..

Thank you

Edited by weszzer

Share this post


Link to post
Share on other sites

Your code:

#include <date.au3>
@YEAR & @MON & @MDAY - 1 & ".csv -D " & @YEAR & "/" & @MON & "/" & @MDAY - 1)
Global $gsDayAdd = _DateAdd("D", -1, _NowCalcDate())
Global $gsCommandString = _RunBuildCommandString(@ComSpec, "/k", "extract.exe", "-I", $gsDayAdd)
Global $giPID = Run($gsCommandString, "", @SW_SHOW)
Global $ghCMDWnd = _getCMDWin($giPID)

If @error Then
    ConsoleWrite("Error: " & @error & @CRLF)
    Exit 101
EndIf

Should not run at all...

The line under #include <data.au3> is invalid (as well as not needed, use the _DateAdd()/_NowCalcDate() method).


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

 I'm back working on this code again.Sorry my mistake. I haven't check the code properly..

Many Thanks SmOke_N

Edited by weszzer

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Hi there..

How to convert this yyyymmdd (20150128) format?

At the moment Igetting 2015/01/28 which is correct. I would also to use the data as file name but the " / " is not accepted as file namn

part of the code:

$gsDayAdd = _DateAdd("D", -1, _NowCalcDate())

I found this somewhere in forum.

 

But it didn't send to cmd I like I my Send( " & $DateSave & ")

Global $aDate, $aTime
$DateSave=_DateTimeSplit(_DateAdd("D", -1, _NowCalcDate()), $aDate, $aTime) ; -1 (minus one day)
ConsoleWrite(StringFormat( "%04s_%02s_%02s", $aDate[1], $aDate[2], $aDate[3]) & @CRLF)
Edited by weszzer

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

The yyyymmdd is the 2015/01/28 format.  It just isn't showing you the delimeter in yyyymmdd.

Use StringReplace:

MsgBox(0, 0, StringReplace(_DateAdd("D", -1, _NowCalcDate()), "/", "_", 0, 1)))

Or StringRegExpReplace:

MsgBox(0, 0, StringRegExpReplace(_DateAdd("D", -1, _NowCalcDate()), "\D", "_")))
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

SmOke_N , thank you very much. really appreciate your help and your time.

When I execute the code it opens the cmd window showing a blank screen (only a cursor while the code is processing)

I can see the extract tool at the Task Manager and it's processing and using PID check, once the extract.exe finished I added to close the cmd console

The code is working great..

 I don't want the user to close the cmd window, it will close once the extract.exe doesn't exist on PID process.(which I added on the logic

so instead I would like to send a message to a console like "please wait..."

is this possible..? at the moment only the cursor is visible at the cmd console.

Global $msge=("Please wait..")
$PID_Check=ProcessWaitClose("exract.exe"); check if exe is in pid process
        if $PID_Check = 1 Then ;1=not exist
        Sleep(2000)
        ProcessClose("cmd.exe")
        MsgBox(0, "", "complete")
     ElseIf
         $PID_Check = 0 Then
        Send($msge)
        EndIf

I found this very similar to my code but, not sure how to include this.

Many thanks..

Edited by weszzer

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