use data from another exe (cmd)

Mr_Gee

Active Member
Licensed User
Longtime User
I'd like to use the data generated by cmd.exe in my desktop application,
is this possible...?

I've tried to search the forum, but i couldn't find any concrete answers


thanks
 

Mr_Gee

Active Member
Licensed User
Longtime User
Well..yes
i'd like to get the output (the text that normally is written in the cmd window)
e.g. when i run shell("cmd.exe","ping 192.x.y.z")
i'd like the result in a b4p variable( or object)
This is possible with PHP, it would be very cool if B4P could also do this
 

mjcoon

Well-Known Member
Licensed User
I have a vague recollection that you used to be able to put two commands into the shell on one line, delimited by the "pipe" character (=|), so called because it pipes the stdout of the 1st command to the stdin of the second. I think this was borrowed from Unix. Early usage: "type <file-name> | more"

Would it work with calling a Basic4PPC executable and would that transfer sufficient data in this case?

Mike.
 

Mr_Gee

Active Member
Licensed User
Longtime User
Interesting Mike,
but wouldn't that output the result to a new exe in stead of the application initiating the cmd?

Erel, this is the code for php
B4X:
<?php

    $cmd = "C:\WINDOWS\system32\ping.exe ping 10.16.2.102 -n 1 && exit";        
    exec($cmd, $output);
    print_r($output);
?>
the code is similar to B4P, the only exception is print_r which basically shows the complete array "$output" on the page.
echo $output[5];

would also be an option...
:)
 

agraham

Expert
Licensed User
Longtime User
You can create a small batch file with the command:
ping 192.x.x.x > 1.txt Then run the batch file with Shell. The output will be saved to 1.txt.
If you use the Process object from the Threading library you can wait until the command finishes which makes things easier. P is a Process object. Note that all the filenames are explicitly prefixed by AppPath to make sure that files are where you expect them. I had trouble if I left them out - you may or may not experience the same.
B4X:
   P.New1
   FileOpen(f, AppPath & "\doit.bat", cWrite,, cASCII)
   cmd = "ping 127.0.0.1 > " & AppPath & "\ping.txt"
   FileWrite(f,cmd )
   FileClose(f)
   P.Start(AppPath & "\doit.bat", "")
   P.WaitForExit(-1)
   FileOpen(f, AppPath & "\ping.txt", cRead)
   t = FileReadToEnd(f)
   FileClose(f)
   Msgbox(t)
 

agraham

Expert
Licensed User
Longtime User
It is better to wrap the path with quotes in case you have spaces in it:
Good point Erel! I'm old-fashioned and don't put spaces in my file and folder names - somehow it just doesn't seem right to do that. I've never really got over the demise of 8.3 filenames :).
 
Top