PDA

View Full Version : Capturing OS_ShellExecute Output?



mhillmer
12-09-2007, 22:35
I searched and didn't find many posts on OS_ShellExecute, and the manual only has minimal information on this. I'm assuming the information returned from it is NOT the output, but rather some sort of status code (I get "42" back from nbtscan.exe, and I don't think this is the answer I'm looking for!).

Is the only way to capture the output of a command line executable to redirect to the output to a file and then read in the file? (painful if you are running lots of commands!)

This is what I'm trying to run:


USES "File"
uses "Console"
uses "OS"

Dim FileToLoad as integer
dim MyArray() as asciiz
dim nLines as long
dim nCols as long
dim CountLine as long
dim CountCol as long
dim sTemp AS ASCIIZ

Console_Writeline("Test Console App")

REM Stores.csv is a list of StoreNumber,VLAN1IPAddress,VLAN2IPAddress
PARSE(FILE_Load("M:\Misc\Matt\CanadianInventory\Data\Stores.csv"), MyArray(), $crlf , ",")

nLines = ubound(MyArray(1))
nCols = ubound(MyArray(2))

console_writeline("Total number of lines : " & nLines)
console_writeline("Total number of colums: " & nCols)

for CountLine = 1 to nLines
sTemp = OS_SHELLEXECUTE ( "open", "nbtscan.exe", MyArray(CountLine,2) + "/24", "", %OS_WNDSTYLE_NORMAL)
console_writeline(sTemp)
next


Thanks,
Matt

ErosOlmi
12-09-2007, 23:25
Matt,

what about this one:


USES "File"
uses "Console"
uses "OS"

Dim FileToLoad as integer
dim MyArray() as string '<<<<< Changed to STRING. ASCIIZ are used only with fized size usually
dim nLines as long
dim nCols as long
dim CountLine as long
dim CountCol as long
dim sTemp AS ASCIIZ

Console_Writeline("Test Console App")

REM Stores.csv is a list of StoreNumber,VLAN1IPAddress,VLAN2IPAddress
PARSE(FILE_Load("M:\Misc\Matt\CanadianInventory\Data\Stores.csv"), MyArray(), $crlf , ",")

nLines = ubound(MyArray(1))
nCols = ubound(MyArray(2))

console_writeline("Total number of lines : " & nLines)
console_writeline("Total number of colums: " & nCols)

'---This will contain the full captured buffer
dim sBuffer as string

for CountLine = 1 to nLines
OS_Shell(OS_ENVIRON("COMSPEC") & " /C " & app_sourcepath & "nbtscan.exe " & MyArray(CountLine,2) & "/24 > " & app_sourcepath & "__Out__.txt", %OS_WNDSTYLE_HIDE, %OS_SHELL_SYNC)
'---At every RUN, output will be deleted, so we accumulate into sBuffer
sBuffer += file_load(app_sourcepath & "__Out__.txt") & repeat$(80, "-") & $crlf
next
'---At the end, sBuffer will contain the full scan
msgbox 0, sBuffer


Capturing the output buffer is not so trivial. We need to create a process manually using Win32 API telling we want to have stdout captured into a file handle. Than we need to create a new file handle, close at the end, get the output manually, ...

Much better it seems to me to have a output file redirection into a temp file. When OS_SHELL finish, capture the output and a new loop with a different net scan will start.

What do you think?

Ciao
Eros

ErosOlmi
13-09-2007, 07:24
Please change

dim MyArray() as asciiz
to

dim MyArray() as STRING

Seems there is a problem in PARSE function when dealing with ASCIIZ strings. Not sure but NULL terminator is not added.

mhillmer
13-09-2007, 17:01
Yes, this is a little longer, but works very well. I understand it isn't trivial to capture the output, but you seem to have worked so many other miracles in this little language, that since I didn't see it in the docs, I just had to ask.

Thanks again,
Matt

ErosOlmi
13-09-2007, 17:34
Well, we are making our best for the "impossible" but we are not yet organized for "miracles" ;D

Feel free to ask whatever you need here. That is a must.