Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read a text-file line by line

381 views
Skip to first unread message

Rene

unread,
Feb 19, 2001, 9:37:59 AM2/19/01
to
Here's my problem:

I have generated a TXT file with one 9-digit code on each line. I want each
of these codes to become the %1 variable in this command:
iexplore.exe http://www.address.com/~%1. After this command, the batch file
should wait for a keypress and process the code on the next line of the TXT
file and so on.

I'm not asking for you to generate such a batch file for me. My main
question is: "How do you "read" a TXT file line by line and output each
single line as a variable in a command (with keypress-pauses in between)"?

Your help is really appreciated,
Thanks,

Rene


Tom Lavedas

unread,
Feb 19, 2001, 12:04:48 PM2/19/01
to

Unless this is in NT/2000, it either requires a third party utility or a
very ugly 'pure' batch hack.

In NT/2000

for /f "tokens" %%a ('d:\path\filename.txt') do call :Doit %%a
goto :EOF

:Doit
start /w http://www.address.com/~%1

In Win 95/98(/ME?), I'd get a copy of something like Strings.com (see:
ftp://ftp.zdnet.com/pcmag/1992/1222/strings.zip). Or, if you're really
adventuresome, you can try my 'pure' batch DoList.bat routine (see:
http://www.pressroom.com/~tglbatch/list.htm).

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/

Outsider

unread,
Feb 19, 2001, 11:02:46 AM2/19/01
to
Tom Lavedas wrote:
>
>
> Unless this is in NT/2000, it either requires a third party utility or a
> very ugly 'pure' batch hack.
>

My my, even Tom is calling batch ugly. This must be the beginning
of the end of batch programming.

--
<!-Outsider//->
MS-DOS 6.22, Windows for Workgroups 3.11, Netscape Navigator 4.08
Your mouse has moved. Reboot computer for changes to take effect.

laura fairhead

unread,
Feb 16, 2001, 10:15:18 PM2/16/01
to

It can't be done simply since there are no commands to support real file
processing in DOS. Often the practical solution is to fall to a 3rd party
utility (SED and AWK being favoured because they are industry standard and
will port easily across platforms).

Using SED you could easily create an auxillary batch program from the
data you wanted to process, maybe;

SET LINE=text from file line 1
CALL processbatchfilename
SET LINE=text from file line 2
CALL processbatchfilename
o
o
o

Is just;

SED "s/^/SET LINE=/;G;s/$/CALL processbatchfilename/" sourcefilename


Then "processbatch" is a batch program that gets call for each line
in turn. You can make "processbatch" the same as the original batch,
using a flag somewhere, testing it and jumping to a part of the original
batch (processbatch "block") if it is true. In fact if your processing
is reasonably small you could simply insert the whole processbatch
block for each line in the auxillary batch.

The same sort of technique is used if you want to do it with "pure"
unsupported batch, except creating the file is perhaps not obvious
to do. The methods known for DOS batch all unfortunately depend
greatly on the type of data in the file (since they are "hacks").

Here is an example shell program that will enable processing
of a file line by line. Set the source data file in the variable
SRC (set at the start), this file cannot contain valid internal dos
commands, valid paths to executables, lines may not contain certain
characters (A-Za-z0-9 will be okay, importantly SPACE is not).

The functional block "READLINE" is called for every line of the
file, with the data in parameter variable %2. You can simply put
the code you desire to be executed in this block.

Use the PAUSE command to wait for a keypress.
Typically to get your own message you can bin the
default one: PAUSE>NUL
Use START command to execute IEXPLORE since otherwise
you are relying on it being in the path (not always
true in a GUI based system).

===================[START OF CODE]====================================

@ECHO OFF
IF !%1==!! GOTO READLINE

:: ------------Set filename of source file.
SET SRC=filenameoffiletoprocess

:: ------------Create $$.BAT batch file that calls us with each
:: ------------line of %SRC% file consecutively

ECHO @PROMPT CALL %0 ! >$.BAT
TYPE %SRC% >>$.BAT
FOR %%_ IN (MD CD) DO %%_ }{
CTTY NUL
SET O=%PATH%
PATH ;
%COMSPEC% /C ..\$.BAT>..\$$.BAT
PATH %O%
SET O=
CTTY CON
CD ..
RD }{

:: ------------Call $$.BAT that will call us @READLINE for
:: ------------each line of %SRC% file

CALL $$.BAT

:: ------------Finished all lines, cleanup and exit

FOR %%_ IN ($.BAT $$.BAT) DO DEL %%_
GOTO OUT

:: ------------READLINE: gets called for each line of %SRC% file
:: ------------with data in %2. Data must not include spaces,
:: ------------be valid DOS internal commands.

:READLINE

:: ------------Ignore any blank lines
::
IF !==!%2 GOTO OUT

:: ------------Process line data as desired
::
ECHO DO SOMETHING WITH "%2%" THE LINE FROM THE FILE
ECHO.

:: ------------Termination point. This label must be kept
:: ------------at the program end.

:OUT

===================[END OF CODE]======================================


bye,

L

>
>
>
>
>
>
>

Tom Lavedas

unread,
Feb 19, 2001, 1:35:52 PM2/19/01
to

No it's very nearly the end of the end ;o)) Besides, I was calling the
HACK ugly, not batch per se.

Dr John Stockton

unread,
Feb 19, 2001, 3:34:25 PM2/19/01
to
JRS: In article <96rb3j$6tn$1...@odysseus.uci.kun.nl>, seen in
news:alt.msdos.batch, Rene <re...@mail.com> wrote at Mon, 19 Feb 2001
15:37:59 :-

One way could be to use a programmable editor to change the file from
*.TXT containing
...
TEXTline
...
to *.BAT containing
...
iexplore.exe http://www.address.com/~TEXTline
pause
...
and execute that.

Otherwise, any programming language should do it - something like
while not EoF(F) do begin
Readln(F, S) ;
SwapVectors ;
Exec('iexplore.exe', 'http://www.address.com/~'+S)
SwapVectors ;
Write(' ? ') ; Readln end ;
which presumably QBASIC can emulate.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt.
Free, DOS/UNIX, from <URL: http://www.pagesz.net/~minitrue/>

Bobby Gage

unread,
Feb 19, 2001, 8:28:16 PM2/19/01
to
It can be done!!!!

OK... This is an old trick I learned a long time ago!!!!


Try this... Place the following batch file into it's own directory then copy
the the with the codes in it to the same directory! Rename the code file to
"codes.txt" or modify the copy command line in the batch file to the filename
of the codes.

What happens is this batch file will change the command prompt to what you are
needing infrount of the other file and also placeing the program for it to call
in frount of that! So it changes the prompt to...

bat1.bat -r http://www.address.com/~

This whay when command.com is calld and the lines from the code file are placed
into (as if entered by hand) the command line you will get lines that look
like...

bat1.bat -r http://www.address.com/~001
bat1.bat -r http://www.address.com/~002

and so on...

The neet thing is that the errors "bad command or file name" are echoed to the
screen and not piped to the file! This prevent's more error messages! But
you will get error message from the prevouse step because your trying to
execute the programs "001" and "002" and so on.. That's why I have removed the
path settings so you don't pick up some stray program or something!

This is then intern redirected to yet another file... script.bat which has
lines that then look the same as above...

This in turn calls bat1 again with the command options

%1= -r
%2= http://www.address.com/~001

and one the bat1 is called with the -r option it then jupst to the line that
executes the command you have asked for

iexplore.exe %2

but I prefure using start when I start 32bit software and if you simply use

start %2

you will automaticly execut the default brouser insted of IE!!!!

Onese are brouser is executed it asks for a pause then it exeits! and returns
to the calling batch file "~script.bat" and it executs it again with the next
webpage and so on...

One ~script.bat is finished it returns to it's calling bat file bat1.bat and
cleans up left over files and terminates!

Hope this helps,
Bobby

ps.. I'm a pore speller!!!!! Sorry!

--------------- Bat1.bat---------------------------
@echo off

if %1. == -r. goto pause

::Remove temp files if thay exist!
if exist script.bat del script.bat
if exist codes.bat del codes.bat

::Backup path settings so other programs are not called by mistake!
set oldpath=%path%
set path=

::Backup the current command prompt options..
set oldprompt=%prompt%
set prompt=call %0 -r http://www.address.com/~

::Replace the "codes.txt" with the filename of the file with the numbers.
copy codes.txt codes.bat
echo exit >>codes.bat

::Use %comspec% because path settings has bean removed...
::Send the codes.bat file as commands to command.com this will replay
::with a command line such as "call bat1 -r http://www.address.com/~001"
::it will then send this data to script.bat as a script file this file will
::be called later. (ignore error messages!)
%comspec% <codes.bat >script.bat


::Replace prompt and path options
set path=%oldpath%
set oldpath=

set prompt=%oldprompt%
set oldprompt=

::Execut script (ignore error messages)
call script.bat

::Remove temp files
del script.bat
del codes.bat
Goto End

:pause
::This will be called by script.bat and will start iexplore.exe and %2
::will have the desired webpages in it! (i would recomend useing
::the start command to run the program!)
::Note! "iexplore.exe" can be removed if you use "start". Windows
::will automatically use the default brouser. Not just IE!
start iexplore.exe %2
Echo:
Echo Press a key when ready for next webpage
Echo:
Pause

:End


---------------------<eof>----------------------

laura fairhead

unread,
Feb 17, 2001, 7:40:26 AM2/17/01
to
On Mon, 19 Feb 2001 20:28:16 -0500, Bobby Gage <BG...@FrederickMD.com> wrote:

>It can be done!!!!
>
gosh!!

>OK... This is an old trick I learned a long time ago!!!!
>

If you had bothered to read the posts in this thread you
will find I posted a batch using this "old trick" of yours.
In fact it is (very) old hat and comes up frequently
(when posters can be bothered with it because we're a bit
bored at the moment) and in fact there are several other
_well known_ ways of doing this in "pure" batch. You just have to
read something of what others around here have already
accomplished.

L

Bobby Gage

unread,
Feb 20, 2001, 8:10:10 PM2/20/01
to

laura fairhead wrote:

> On Mon, 19 Feb 2001 20:28:16 -0500, Bobby Gage <BG...@FrederickMD.com> wrote:
>
> >It can be done!!!!
> >
> gosh!!
>
> >OK... This is an old trick I learned a long time ago!!!!
> >
> If you had bothered to read the posts in this thread you
> will find I posted a batch using this "old trick" of yours.

Well I'm new to this post and I learned this trick a long time ago.. That's why
I called it an old trick. :-)

But.. I did read your post BUT only read the first line.

=====(cut)=============


It can't be done simply since there are no commands to support real file
processing in DOS. Often the practical solution is to fall to a 3rd party

utility (SED and AWK being favored because they are industry standard and


will port easily across platforms).

----------------------------------

But I now see your statement of "simple"!!

O well such is life!

I didn't thing of your use of CTTY to remove the error messages though....

>
> In fact it is (very) old hat and comes up frequently
> (when posters can be bothered with it because we're a bit
> bored at the moment) and in fact there are several other
> _well known_ ways of doing this in "pure" batch. You just have to
> read something of what others around here have already
> accomplished.

I don't really know how to take this.. Sorry if you took my message wrong. But
I didn't say at all that it was not a well known method. I was just making a
statement that I it's old to me and I have not used it for a long time!


Later,
Bobby


Rik D'haveloose

unread,
Feb 20, 2001, 5:17:11 PM2/20/01
to
Tom Lavedas wrote:
> Outsider wrote:
>> Tom Lavedas wrote:
>>>
>>> Unless this is in NT/2000, it either requires a third party
>>> utility or a very ugly 'pure' batch hack.
>>
>> My my, even Tom is calling batch ugly. This must be the beginning
>> of the end of batch programming.
>
> No it's very nearly the end of the end ;o)) Besides, I was calling the
> HACK ugly, not batch per se.

<g>
Or how one can name a feature an ugly hack ???
</g>

Please, if only NT answers are given, redirect also to
alt.msdos.batch.NT.
(i even guess in this case that no NT-solution was asked/intended)

--
Lieve - Ri(n)ksken(s)
TUF Greetings from Rumbeke, Belgium

Please only use 1 e-mail adress to reply !


Bill Marcum

unread,
Mar 1, 2001, 12:59:33 PM3/1/01
to

Dr John Stockton wrote in message ...

>One way could be to use a programmable editor to change the file from
>*.TXT containing
> ...
> TEXTline
> ...
>to *.BAT containing
> ...
> iexplore.exe http://www.address.com/~TEXTline
> pause
Or instead of a .bat file you could create a .htm file, and visit all
the links with one run of iexplore...

Tom Lavedas

unread,
Mar 1, 2001, 2:58:49 PM3/1/01
to

In NT/2000, try ...

for /f %%a in (%1) do call :Sub %%a
goto :EOF

:Sub
start http://www.address.com/~%1
pause

Something like this should work in Win 95/98 ...

@%2 echo off
md @tmp@
echo prompt $ call %0 %1 goto:2nd;> @tmp@\~tmp.txt
type %1 >>@tmp@\~tmp.txt
echo exit $>>@tmp@\~tmp.txt
ctty nul
%comspec% /e:2048/k < @tmp@\~tmp.txt | find/v "$" > @tmp@\~tmp.bat
ctty con
for %%v in (@tmp@\~tmp.bat del) do call %%v @tmp@\~tmp.*
for %%v in (rd.\@tmp@ goto:End) do %%v
:2nd
start http://www.address.com/~%3
pause
:End

Provide the fully qualified filespec to the list file as the single
command line argument in either case.

0 new messages