WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Command Line Stdin Stdout


Question:

Any way to read/write stdin and stdout?

Well, I guess what I was thinking of is programs that do this sort of stuff:

DIR *.DAT | PROGRAM PAR1 PAR2 >T.TXT
Here, the program processes some text from standard input to produce text on standard output. I am not interested in interacting with the user via the console. If I want to ask the user a question, I'll pop a dialog.

This can be simulated adequately by creating temporary files, but it is never a clean and simple as this. And you need the darn START/WAIT command.

Another handy thing would be ability to read and write the current set of win2K environment vars:

SET FRED=123
PROGRAM FRED SALLY
ECHO %SALLY%
Here, the program scans two var names from it's command line. It gets the present value of the first var, does something, and stores the answer in the second var. The calling script can then use that output value.

I've yet to figure a way to store values in the current vars.

Generally, the application is various small functions on various servers. I'd like to be able to use simple .BAT files, with the occasional helper program written in winbatch.

Answer:

First create a script called myscript.wbt
hKernelDll = DllLoad(StrCat(DirWindows(1), "kernel32.dll"))
hStdIn = DllCall(hKernelDll, long:"GetStdHandle", long:-10)
hStdOut = DllCall(hKernelDll, long:"GetStdHandle", long:-11)
hBytesBuf = BinaryAlloc(4)

If hStdIn != -1
dwFileSize = DllCall(hKernelDll, long:"GetFileSize", long:hStdIn, lpnull)
hDataBuf = BinaryAlloc(dwFileSize)
DllCall(hKernelDll, long:"ReadFile", long:hStdIn, lpbinary:hDataBuf, long:dwFileSize, lpbinary:hBytesBuf, lpnull)
dwBytesRead = BinaryPeek4(hBytesBuf, 0)
BinaryEodSet(hDataBuf, dwBytesRead)
data = BinaryPeekStr(hDataBuf, 0, dwBytesRead)
Else
dwFileSize = 0
hDataBuf = BinaryAlloc(dwFileSize)
data = ""
Endif

Message("stdin", data)

If hStdOut != -1
DllCall(hKernelDll, long:"WriteFile", long:hStdOut, lpbinary:hDataBuf, long:dwFileSize, lpbinary:hBytesBuf, lpnull)
Endif

BinaryFree(hDataBuf)
BinaryFree(hBytesBuf)
DllFree(hKernelDll)

Now go to the command shell and type:

DIR *.* | C:\PROGRA~1\WINBATCH\SYSTEM\WINBATCH.EXE C:\TEMP\MYSCRIPT.WBT>T.TXT
You should see the results displayed in a Winbatch message box.

UserReply:

Holy dreck Batman -- it works!!! THANKS!!!
TYPE BIGTEXTFILE.TXT | MYPROGRAM | MORE
worked fine on WinXPPro SP1, winbatch 2004A.

Also works on win2k.

Also works on Win98se, but requires START:

TYPE FILE.TXT | START/W PROGRAM | MORE
These work fine for me in Win95 (OSR2) and XP:
echo Hello | myprogram > output.txt
myprogram < input.txt > output.txt
Also when replacing "myprogram" with "winbatch.exe myprogram.wbt".

Article ID:   W15962
File Created: 2004:03:30:15:42:04
Last Updated: 2004:03:30:15:42:04