Jump to content
Sign in to follow this  
am632

get result from command line without other outputted text

Recommended Posts

am632

Hi,

Im trying to get a value from an adb command which is run from a bat file, and I want it to display the result without displaying other text. The code I have so far is...

#RequireAdmin
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <GuiEdit.au3>

$Form1 = GUICreate("Form1", 337, 221, 192, 124)
$LogBox = GUICtrlCreateEdit("", 16, 8, 305, 193)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

Run(@ComSpec & " /c " & "adb" & " " & 'kill-server', @ScriptDir, @SW_HIDE)
$PID = Run(@ScriptDir & "\adb.bat >log.txt", @ScriptDir, @SW_HIDE)
    Do
        Sleep(200)
        $line = FileRead('log.txt')
        GUICtrlSetData($LogBox, $line)
        _GUICtrlEdit_Scroll($LogBox, $SB_SCROLLCARET)
    Until ProcessExists("cmd.exe") = @error
ProcessClose($PID)

RunWait(@ComSpec & " /c " & "adb" & " " & 'kill-server', @ScriptDir, @SW_HIDE)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

the adb.bat file contains, "adb shell getprop ro.product.name" without quotes.

Im not currently getting any text in the logbox although I have done with other scripts in the past so I dont quite know why, but I want to just display the result and not the other text generated by running adb (daemon not running etc)

Please can someone help with this please?

Thanks

Share this post


Link to post
Share on other sites
MHz

Perhaps better to do the FileRead once the process is done.
This line

Until ProcessExists("cmd.exe") = @error

seems odd as the previous function call of _GUICtrlEdit_Scroll is not documented as setting error. So that is unreliable to test @error.

If this is a quick process than may I suggest you use StdoutRead to get the data and so you are not reading a file at all.

#RequireAdmin
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <GuiEdit.au3>

$Form1 = GUICreate("Form1", 337, 221, 192, 124)
$LogBox = GUICtrlCreateEdit("", 16, 8, 305, 193)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

; create a test file
If Not FileExists('adb.bat') Then
    FileWrite('adb.bat', '@echo off' & @CRLF & 'echo Hello %*' & @CRLF)
EndIf

Run(@ComSpec & " /c " & "adb" & " " & 'kill-server', @ScriptDir, @SW_HIDE)
Global $stream, $Pid = Run('"' & @ComSpec & '" /c adb.bat am632 1 2 3 4 5 6 7 8 9 0 ...', @ScriptDir, @SW_HIDE, 8); merged stream
Do
    Sleep(10)
    $stream &= StdOutRead($pid)
Until @error
GUICtrlSetData($LogBox, $stream)
_GUICtrlEdit_Scroll($LogBox, $SB_SCROLLCARET)

RunWait(@ComSpec & " /c " & "adb" & " " & 'kill-server', @ScriptDir, @SW_HIDE)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

It creates a test file named adb.bat if it does not exists. I get the output of

Hello am632 1 2 3 4 5 6 7 8 9 0 ...

which is added to the edit ctrl.

Share this post


Link to post
Share on other sites
Celtic88

This script will help you  :)

#NoTrayIcon
Opt("GUIONEVENTMODE", True)

Global $LASTncmd, $CmdProces

GUICreate("By Mrbenkali", 650, 424)
GUISetOnEvent(-3, "_EXIT")
Global $btnGo = GUICtrlCreateEdit("", 0, 0, 650, 424)
GUICtrlSetLimit(-1, 999999999)
GUICtrlSetFont(-1, 9, 400, 0, "Lucida Console")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
Local $ENTER = GUICtrlCreateDummy()
GUICtrlSetOnEvent($ENTER, "_Go")
Local $AccelKeys[1][2] = [["{ENTER}", $ENTER]]
GUISetAccelerators($AccelKeys)
GUISetState()

_Go()

While 1
    Sleep(1000)
WEnd

Func _Go()
    Local $REDCTRL = GUICtrlRead($btnGo)
    Local $STRCMD = StringTrimLeft($REDCTRL, $LASTncmd)
    $LASTncmd = StringLen($STRCMD)
    
    $Rcmd = _Runcmd($STRCMD)
    
    GUICtrlSetData($btnGo, StringTrimRight(GUICtrlRead($btnGo), $LASTncmd) & $Rcmd)
    $LASTncmd = StringLen(GUICtrlRead($btnGo))
EndFunc   ;==>_Go

Func _Runcmd($Command)
    Local $Cmdaut
    If ProcessExists($CmdProces) = 0 Then
        $CmdProces = Run(@ComSpec, @HomeDrive, @SW_HIDE, 7)
    Else
        StdinWrite($CmdProces, $Command & @CRLF)
    EndIf
    Local $Timecmd = TimerInit()
    Do
        $Command = StringRight($Cmdaut, 1)
        $Cmdaut &= StdoutRead($CmdProces)
        $Cmdaut &= StderrRead($CmdProces)
        If $Command = ">" Then ExitLoop
    Until TimerDiff($Timecmd) > 2000
    Return $Cmdaut
EndFunc   ;==>_Runcmd

Func _EXIT()
    Exit
EndFunc   ;==>_EXIT
Edited by Mrbenkali

Share this post


Link to post
Share on other sites
am632

Hi,

thanks for the replies. I like your script Mrbenkali but its not quite what I need, thanks anyway. however your script MHz doesnt seem to work for me, I have tried copying the script verbatim but its not displaying anthing in the logbox, I can see adb runs in task manager etc and the bat file is created correctly but i dont know if the command is running or if its just not displaying in the logbox. Any ideas?

Thanks

Share this post


Link to post
Share on other sites
MHz

The bat file runs OK on Win 7 for me with the Stdout text added to the Gui. So as for not working for you I am surprised. Looks like you are wanting to use Android Debug Bridge from the ADT SDK. I am guessing you have read this page about output. I am not sure if it is relevant to this or not but it does mention Stdout & Stderr.

Just running adb could be running adb.bat instead of presumably adb.exe so you may need to watch for that.

You could try a small test with this to see if the prompts show any data

RunWait('"' & @ComSpec & '" /k adb kill-server', @ScriptDir)
RunWait('"' & @ComSpec & '" /k adb shell getprop ro.product.name', @ScriptDir)
RunWait('"' & @ComSpec & '" /k adb kill-server', @ScriptDir)

You use kill-server twice for some reason. Then you could try modifying them to print to a log file and see if they can do that. If so then StdoutRead may work.

So, you could try this one which is little different though not using a bat file but direct commands to adb.

#RequireAdmin
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <GuiEdit.au3>

$Form1 = GUICreate("Form1", 337, 221, 192, 124)
$LogBox = GUICtrlCreateEdit("", 16, 8, 305, 193)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

RunWait('"' & @ComSpec & '" /c adb kill-server', @ScriptDir, @SW_HIDE)
Global $stream, $Pid = Run('"' & @ComSpec & '" /c adb shell getprop ro.product.name', @ScriptDir, @SW_HIDE, 8); merged stream
Do
    Sleep(10)
    $stream &= StdOutRead($pid)
Until @error Or Not ProcessExists($pid)
GUICtrlSetData($LogBox, $stream)
_GUICtrlEdit_Scroll($LogBox, $SB_SCROLLCARET)

RunWait('"' & @ComSpec & '" /c adb kill-server', @ScriptDir, @SW_HIDE)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

:)

Share this post


Link to post
Share on other sites
am632

Hi,

thanks for the reply. I am getting a result with that last script - I have actually already been able to acheive this but its specifically the result of the adb command I want displayed - for example the product.name and not the '* daemon not running. starting it now on port 5037 *
* daemon started successfully *' Sorry if I wasn't clear with this. I didnt use the std commands, I cant remember how I got there now but I do need to learn how to use std properly. What would you think the best solution for this would be? Would it be possible to output the results to a txt file and have autoit remove the '* daemon not running. starting it now on port 5037 * * daemon started successfully *' text leaving just the result which I could read back? or is this stupid?

Thanks

EDIT: Oh yeh, the reason I use kill-server twice is just to make sure there are no adb processes running before the script is executed - ie if the user has already been using adb and not exited it.

Edited by am632

Share this post


Link to post
Share on other sites
MHz

The last script in post >#5 is using Stdout.

Or are you referring to the 3 line script in the same post?

I am guessing you get output to the command prompt and not to the edit control in the Gui window.

If you can get something to output, then there is a chance. Your initial code redirected to log.txt.

Did log.txt have anything in it on the test? I know the Gui showed nothing, but log.txt is unknown.

Need you to get some accurate results so something can be created to handle it. Filtering strings is possible to get just the text of interest.

:)

Share this post


Link to post
Share on other sites
MHz

I downloaded the 460 MB SDK so I could try it myself. Seems on the command line, you may need to redirect Stderr to create a log file with output in it. So you would do this

adb > log.txt 2>&1

which redirects Stderr to Stdout. Then you get something to read.

With AutoIt, I used this

GUICreate('Android Debug Bridge', 600, 400)
$edit = GUICtrlCreateEdit('', 10, 10, 580, 380)
GUISetState()

Global $stream
$pid = Run('"' & @ComSpec & '" /c adb.exe', @ScriptDir, @SW_HIDE, 8); or for log file use  > log.txt 2>&1
Do
    Sleep(10)
    $stream &= StdOutRead($pid)
Until @error
GUICtrlSetData($edit, $stream)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case -3; $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

I run it and get this, which will show help

post-2709-0-70461400-1380805309_thumb.gi

STDIO Report:

STDIO flag 2 (StdOutRead) does not work. STDIO flag 4 (StdErrRead) works. STDIO flag 8 (StdOutRead) works which is merged StdOut and StdErr.

As stated about the SDK, StdOut is sent to /DEV/NULL. So, only StdErr stream is available.

The use of the command  adb shell getprop ro.product.name  outputs

error: device not found

as the emulator probably needs to be running.

So it shows as working on Windows 7 Pro SP1. :)

Edit:

Using Android Debug Bridge version 1.0.31

Updated STDIO Report with correction.

Edited by MHz

Share this post


Link to post
Share on other sites
am632

Hi,

Thanks for the help & examples.

I have my edit control displaying text from a log.txt file but how would I go about 'Filtering Strings' as you say - to cut out the junk basically?

Thanks

Share this post


Link to post
Share on other sites
MHz

Read log.txt. Use String* functions to modify the read string as required, and then GuiCtrlSetData to the edit control. Many string functions exist and most useful for this could be StringRegExp and StringRegExpReplace. The grabbing of text or removal of text depends on the text and the patterns needed. If you need help with that then you may need to give some samples and mention what is needed. :)

Share this post


Link to post
Share on other sites
am632

kwl, thanks for the information :) I will play with this tonight

Thanks

Share this post


Link to post
Share on other sites
am632

Hi,

Thanks for all the help, I have got the results I wanted - gets the text im looking for and sets it in a variable :)

The only thing I noticed is lets say I use the command...

StringRegExp($readlog, "test") Then
    $readword = "test"

this works fine if the word found is 'test' but if the word in the txt file is 'testing' it still sets the variable to test because the string is still in the file. Is there a way to 'fix' this?

Thanks

Edited by am632

Share this post


Link to post
Share on other sites
gruntydatsun

findTest("string contains test and should succeed")
findTest("String contains testing and should not succeed")

Func findTest($string)
    if StringRegExp($string, "\htest\h") Then
        $readword = "test"
        msgbox(1,"SUCESS","String was :  " & $string & @CRLF & "$readword = " & $readword)
    Else
        msgbox(1,"FAIL", "Word 'test' not found in string:  " & @CRLF & $string)
    EndIf
EndFunc

Drop a h in on either end of the regex so it'll find string if it's got a space or tab or carriage return etc on either side of it.

edit:

StringRegExp($string, "[h|.|,]test[h|.|,]") will find it if it's got a full stop or comma after it.  Just add the things you can accept in the square brackets separated by a |

Edited by gruntydatsun

Share this post


Link to post
Share on other sites
am632

Hi,

Thanks for the help, /hword/h seemed to work for me, it was used for detecting firmware version - as it wasn't making the difference between for example 4.1 & 4.2.1, so if my code checked for 4.1 first and the firmware detected was 4.2.1 is was labeling it as 4.1

So in short it seems to have fixed my problem :) thanks

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  

×