• Please review our updated Terms and Rules here

DOS Batch files & STDIN

mbbrutman

Associate Cat Herder
Staff member
Joined
May 3, 2003
Messages
6,418
I know how to redirect input into a DOS program, but I'd like to find a way to create a batch file that has the standard input that I would normally redirect to the program already included in the batch file.

For example, instead of this:

ftp < script.txt

I would like to have an ftp.bat that runs the ftp.exe program and has the required stdin stream contained within. Something that looks more like this:

ftp << EOF
stdin stream goes here
EOF


Does such a mechanism/syntax exist under DOS?


Mike
 
I know how to redirect input into a DOS program, but I'd like to find a way to create a batch file that has the standard input that I would normally redirect to the program already included in the batch file.

For example, instead of this:

ftp < script.txt

I would like to have an ftp.bat that runs the ftp.exe program and has the required stdin stream contained within. Something that looks more like this:

ftp << EOF
stdin stream goes here
EOF

Does such a mechanism/syntax exist under DOS?

Mike
Does
ECHO <stdin stream> | FTP
work for ya?
 
I was kind of hoping to get the required stdin data included in the batch file as data. Otherwise, what you have there is morally equivalent to redirecting stdin from a text file.

I have seen a crude example that goes something like this:

Code:
Goto Begin
Ignore these past two error messages ...
d 0
jmp 1c0
(other debug commands here ...)
quit
:Begin
Echo Start of batch file ...
debug < %0.bat


This example batch file feeds itself as input to debug. The first two lines generate errors, but after that it works well enough. I'd like something more general though .. this code was a quick hack, and it works because debug rejects the first two lines as invalid input.

Mike
 
I was kind of hoping to get the required stdin data included in the batch file as data. Otherwise, what you have there is morally equivalent to redirecting stdin from a text file.

Mike

*Morally* equivalent??? This is a *moral* question??? ;-)

Duh... I guess I don't understand; I thought the data (<stdin stream>) *is* included in the batch file... did the <>s confuse things or am I missing the point?

ECHO help |ftp

But I suspect you're wanting a longer stream... ;-)
 
Last edited:
Would this work?
Code:
ECHO first line > input.txt
ECHO second line >> input.txt
ECHO third line >> input.txt
PROGRAM < input.txt
DEL input.txt
 
Would this work?
Code:
ECHO first line > input.txt
ECHO second line >> input.txt
ECHO third line >> input.txt
PROGRAM < input.txt
DEL input.txt

That's how I'd do it for multiple lines (or 'TYPE input.txt | PROGRAM'), but for some reason Mike doesn't want to redirect stdin from a text file; maybe his environment is read-only.
 
Last edited:
It's possible to do what Mike wants in MS-DOS, but it's not simple. COMMAND.COM maintains a control block for every active batch file (Batch file control block or BCB)--more than one batch file can be active, if for instance, a CALL was used to invoke a batch file from within another.

But you can manipulate the pointers in a BCB to get COMMAND.COM to skip (forward or backward) within a batch file and do other strange stuff.

More info here...

I don't know if this would work with 4DOS or under NT's CMD; probably not.
 
ftp << EOF
stdin stream goes here
EOF

Does such a mechanism/syntax exist under DOS?

Well that's how you would do it under a LINUX/UNIX shell. Aren't there DOS ports of bash (or some other UNIX shell) out there?

I haven't tried them though
 
Get my DeLorean and check the flux capacitor...

What you're asking for used to be pretty much standard with mainframe batch-oriented punched-card systems and has been around for perhaps 50 years.

As an example, take DOS/360 JCL or even predecessors like 7090 IBSYS or even 1620 Monitor IID. You'd have a deck of cards for a job that was something like this:

//JOB
//EXEC program
....data for program...
/* end-of-data marker
//EXEC next-program
....data for next-program...
/*
//EXEC another-program
...same sequence as above...
/*
/& (eof marker)

Why not write your own CLI to handle this?
 
You have a Flux capacitor? I upgraded mine to a Mr Fusion ...

The batch file being able to provide it's own STDIN is just to make invoking it slightly easier - it's not a hard requirement. Creating the tmp file was a good workaround, if I can find a way to generate a unique name that probably not conflict with an existing file.

I thought that MS-DOS could do this, but I guess I'm tainted by too many years of Unix. And lately, I'm swamped with too much else going to to persue it.

When I come up with something elegant I'll post it ..


-Mike
 
DOS Batch files STDIN

DOS Batch files STDIN

Thanks for the help guys.
Well, I did look up how to write into text files using the Batch file. I just cant figure out one thing - I cant seem to be able to write the contents of a system variable into the text file. Here is what Ive got so far:

echo off
cd c:
echo time >> progress.txt
set /p doingWhat = What are you doing?
pause
echo At this time, you were doingWhat >> progress.txt
 
Thanks for the help guys.
Well, I did look up how to write into text files using the Batch file. I just cant figure out one thing - I cant seem to be able to write the contents of a system variable into the text file. Here is what Ive got so far:

echo off
cd c:
echo time >> progress.txt
set /p doingWhat = What are you doing?
pause
echo At this time, you were doingWhat >> progress.txt

How about

time /t >>progress.txt

as time's an internal command, not a variable?
 
My solution

My solution

I know how to redirect input into a DOS program, but I'd like to find a way to create a batch file that has the standard input that I would normally redirect to the program already included in the batch file.

For example, instead of this:

ftp < script.txt

I would like to have an ftp.bat that runs the ftp.exe program and has the required stdin stream contained within. Something that looks more like this:

ftp << EOF
stdin stream goes here
EOF


Does such a mechanism/syntax exist under DOS?


Mike

I see this is an old thread but it pretty much exactly matched what I was looking for.. After reading this, I came up with the solution on my own.

Most of my background is in Unix so I understand what the original poster "Mike" was asking.

My dos equivalent in a batch file is (Windows7 cmd.exe "dos prompt")
@echo off
( echo user username password
echo ls -R outfile.txt
echo quit ) | ftp -n servername

This was functionally equivalent to doing this in a sh, bsh, ksh, etc in Unix

ftp -n servername <<EOF
user username password
ls -R outfile.txt
quit
EOF


I hope this helps future viewers.
 
Last edited:
The ( . . . ) construct might be a syntax extension of Windows 7 but i don't believe it would work under true (original) DOS.


However, when thinking a little about it, i could imagine a small tool could be written, which implements some kind of equivalent to the UNIX shell "here documents". In the latest versions of two of my tools (SELECT and FORM) of my DOSUTILS package (which is available here: http://www.bttr-software.de/products/jhoffmann/) i already implemented something similar.

It looks a little bit different compared to its UNIX counterpart but serves the same purpose. The basic idea is to prefix the lines which are intended to go to the program rather than to command.com with a special prefix, which causes the batch processor to regard them as comment and in addition help the program which reads the batchfile to identify the lines it should read.

Currently this only works for the two above mentioned programs which are especially designed for that pupose. But one could write another little tool (let's call it "heredoc") which just reads such lines, removes the prefixes and copies the rest to its own stdout channel. This could then be piped into any other program which is capable of taking commands from stdin. As an example, this is how it could look like:


rem-1 user username password
rem-1 ls -R outfile.txt
rem-1 quit
heredoc rem-1 %0 | ftp

The program "heredoc" (which is the one that would have to be written) looks for lines prefixed with a text specified by its first parameter (in this case "rem-1") in a file specified by its second parameter, in this case represented by "%0" which is the name of the batch-file itself. After stripping off the prefixes it sends those lines to "ftp". As the respective lines all start with the character sequence "rem", command.com regards them as comments and ignores them.

But who really wants such a program?
 
I think a lot of the confusion here is calling it stdin -- There is no such thing as "stdin" on the DOS command line! Dimes to dollars that's what's confusing MikeS, as stdin is a C construct and has nothing to do with what it's called in the host OS, particularly DOS. If you don't program C, "stdin stream" is gibberish you'd assume is a literal. What I think you are looking for is redirecting CON (console) to your program...

Just like the old COPY CON test.bat copied console input to the keyboard.

Hence:

TYPE CON > ftp

If you want the keyboard sent to the FTP program.

Swapping CON as needed for any of the following:
COM1, COM2, COM3, COM4, AUX

The values PRN, NUL and LPTx wouldn't do anything, in fact would probably lock up the system since you'd have no way to send a EOF. (^Z)

But stdin, stderr, etc -- those are C concepts and have absolutely NOTHING to do with a DOS environment.

That or I'm completely missing the question too.
 
Hmm. There is very much the concept of "stdin" and "stdout" in DOS on the command line. Otherwise the DOS manuals would not refer to them! My DOS 2.1 manual clearly defines them in chapter 1 in a section called "Redirection of Standard Input and Output Devices".

The ability to link the output of one program to the input of another or to redirect where output goes from a default (the console) is an operating system concept. Not a C concept. The terminology is borrowed from Unix, as were many other things in DOS 2.x.


Mike
 
Well, to be accurate, you're both right.

"stdin", "stdout" and "stderr" are not devices, per se, in the same sense that CON or PRN are. They're file handles that have predefined meanings and which are made available to programs. Their usage is called out in the application notes to DOS 2 (prior to DOS 2, there were no handles, only a CP/M-type API) and arose, again as the "Xenification" of DOS. Since these handle definitions are inherited by child processes, it's very convenient for a top-level program to redefine the standard handles passed to a spawned child process.

Any application is perfectly free to redefine them--and that's basically the way that redirection works. The big difference between DOS and Unix in implementation, however is the issue of "piping" the output of one program to the input of another. DOS, being single-tasking writes the output of a "piped" program to a file and waits until the originating program has completed before passing the contents of the file to the receiving program. In Unix, the linkup is real-time; that is, both programs are loaded in memory and the output of one is passed to the input of the other as it happens.
 
There is no such thing as "stdin" on the DOS command line! Dimes to dollars that's what's confusing MikeS, as stdin is a C construct and has nothing to do with what it's called in the host OS, particularly DOS.
I don't think I was confused then, but I sure am now...Are you just quibbling over using 'stdin/stdout' instead of 'standard input/standard output'?
What I think you are looking for is redirecting CON (console) to your program...
No, I think what Mike is looking for is a way to send text contained within a batch file to a program.
Just like the old COPY CON test.bat copied console input to the keyboard.
To the keyboard??? Could you possibly have meant "to a file"?
Hence:
TYPE CON > ftp
If you want the keyboard sent to the FTP program.
No, I think you would send whatever you type to a file called "FTP"
Swapping CON as needed for any of the following:
COM1, COM2, COM3, COM4, AUX

The values PRN, NUL and LPTx wouldn't do anything, in fact would probably lock up the system since you'd have no way to send a EOF. (^Z)
Well, duh! Using PRN/NUL/LPT for standard input isn't going to get you very far, but sending standard output to them is very useful indeed!

But stdin, stderr, etc -- those are C concepts and have absolutely NOTHING to do with a DOS environment.
See Mike B's comments (and Chuck's, who beat me) above; and of course there is also the pipe |
 
Last edited:
Back
Top