• Please review our updated Terms and Rules here

DOS Batch files & STDIN

With apologies for once again resurrecting this dinosaur thread, I found the forum via a google search trying to solve a similar problem, so I thought I'd share what I've managed to put together.

How about this - this batch file will produce 'stdout' of whatever is contained between the two markers at the top of the batch file.

Code:
@echo off
REM Between here and the end data label, there should be no blank lines
goto enddata
help
quit
:enddata
Rem and here's your batch file
set foundstart=
for /f "tokens=* delims=" %%i in (%0) do (
if "%%i"==":enddata" goto endloop
if defined foundstart echo %%i
if "%%i"=="goto enddata" set foundstart=true
)
:endloop
ftp < %0

That seems to do what was asked, if I've understood the question correctly.

A few errors get produced at the end here as the output of ftp is being fed back to itself.
Where that's a risk, the last line can be changed to:
Code:
(ftp < %0 ) > nul

With the downside that all console output is muted.

The problem I was trying to solve initially, which I was able to do with the help of this thread was to be able to paste a multiple line block into a prompt in a batch file and then have it process each line in turn.
I was able to do this with the following:

Code:
for /f "tokens=* delims=" %%i in ('type con') do (
REM Process each %%i in this block of script
)

The block of pasted text is terminated by Ctrl-Z and enter. In my case, I wanted to paste in some drive mappings and have them all map up with Net Use, but you could do whatever was necessary.

Hope this is of help to someone.

David
 
Last edited:
Back
Top