Redirection of DOS output

Posted on 1997-04-14
Last Modified: 2010-04-06
I am writing a Delphi 1 shell for a 16-bit dos grep.

How do I, without calling a dos batch file, use delphi code to redirect the output of a call to a dos command to a file?

An equivalent dos command line would be
'grep -flc foo bar.txt > baz.txt'

It is the '> baz.txt' portion for which I seek an answer.

Question by:laowai

Expert Comment

ID: 1335389
Is the DOS GREP your own creation?  I'm guessing no, else the solution would be fairly simple.

Author Comment

ID: 1335390
No, the 16 bit grep is not my own.  However, sample code for an *equally fast* grep would be a handsome substitute for an answer to the first question.  

I have found two problems with the following algorithm, implemented in 16 bit Delphi:
    1) Take search string
    2) Take line from file searched.
    3) If search string is in searched line, return searched line
    4) Repeat until no more lines in searched file.

Problem 1: It is not as quick as grep.

Problem 2: It bails on large files.  (I am specifically working with dictionaries, 9mbs etc.)  16 bit Grep, of course, doesn't bail.  What's it doing differently?

Related problem: The grep delphi components going around all only return whether the search file contains the string; they are not as versatile as grep.  The majority of these components are wrappers for the old regexp.dll.


Expert Comment

ID: 1335391

I suspect the fast versions of grep are constructing a
finite state machine to check a line for the pattern, rather
than just checking it in a naive way.

They might be reading the file in large blocks also, checking the entire block for the pattern, then retro-actively figuring out where the line was when the pattern is found.

You should be able to get a free copy of a grep code from Gnu or
a Linux site.  Also, DJGPP includes egrep and fgrep, but they
are 32-bit, even though they run on top of DOS.

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Expert Comment

ID: 1335392
If you need grep you might just have it. The source code for grep was included with Pascal7 as a Turbo Vision example.

Accepted Solution

erajoj earned 150 total points
ID: 1335393
This is a really simple problem:
Just use something like this:
  WinExec('command /c grep -flc foo bar.txt > baz.txt'...

You can also use CreateProcess if you want to wait for
the grep to finish before your shell continues.

Hope this helps.

Expert Comment

ID: 1335394
I assume you are creating a dos program.

You have 2 options :

a) Use to execute your program and redirect the output. This is simple, but uses memory for additional, and you can't control output to STDERR (see below).

b) Pipe the output our self :
Piping output from at dos program (using > tofile.txt) works,
because the dos program writes output to standard output (STDOUT) and/or standard error (SDTERR). When a program is invoked, these files has already been opened by, so all you have to do is create a new file (or two) for the output, and then set then handle of STDOUT (and optionally STDERR) to the new handle(s).
Here is an example from a C program of mine (in Pascal notation):

  STDOUT = 1
  STDERR = 2

// preserve old stdout and stderr
OldOutHandle := dup(STDOUT);
OldErrHandle := dup(STDERR);

// create file for output
f = filecreate ('MyFile.TXT');

{ redirect standard files to output file
  by duplicating the file handle onto
  the file handle for standard files. }

dup2(f, STDOUT);
dup2(f, STDERR);

// close the handle for output file

 Here, you execute the program

// restore original standard handles
dup2(OldOutHandle, STDOUT);
dup2(OldErrHandle, STDERR);

// close duplicate handle for standard files

The C Dup function can be replaced by a DOS call (INT 21H)
with ah = $45, bx = handle.
At return, carry is set if error, AX = error code (4 = to many files open, 6 = invalid handle).
If OK, ax is the new handle (result of dup)

The C Dup2 function can be replaced by a DOS call (INT 21H)
with ah = $46, bx = handle of your file, cx = handle to duplicate.
At return, carry is set if error, AX = error code (4 = to many files open, 6 = invalid handle).

Let me know, if you need further help

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question