Using QBASIC to "Prepend" to All Lines in a File


QBASIC allows a straightforward solution to the problem of how to add any desired text to the beginning of all lines in a file. 

Quick: What's the opposite of "Append"? No, it isn't "Prepend". Prefix? Maybe I should switch to Esperanto...
What is needed (and QBASIC can supply) is a way to put whatever we want at the beginning of every line in a list. Yes, this includes the word CALL! That makes processing an entire list easy! Here's one way to do this:

echo OPEN "input.txt" FOR INPUT AS #1 > prepend.bas
echo DO WHILE NOT EOF(1) >> prepend.bas
echo   LINE INPUT #1, dataline$ >> prepend.bas
echo   IF NOT dataline$ = "" THEN PRINT "call process.bat "; dataline$ >> prepend.bas
echo LOOP >> prepend.bas
echo SYSTEM >> prepend.bas
qbasic /run prepend.bas > output.bat
del prepend.bas
The first six lines use redirected ECHO to create the PREPEND.BAS Basic program. The seventh line runs the program. Now let's examine the QBASIC code in the "prepend.bas" file we will generate:

OPEN "input.txt" FOR INPUT AS #1
DO WHILE NOT EOF(1)
  LINE INPUT #1, dataline$
  IF NOT dataline$ = "" THEN PRINT "call process.bat "; dataline$
LOOP
SYSTEM
 

First we OPEN the file we'll be reading. I used INPUT.TXT. You use whatever you want. Notice I never bother to CLOSE the file. I let QBASIC do it for me when it ends. Hey, it saves a line! Next is the DO. It is matched up to the LOOP at the end of the QBASIC program. There's only one way out of this loop, and that's when we hit the end of the input file (And that condition is tested for in the DO line). Now we're into the meat. The LINE INPUT reads a line from the input file. If it turns out to be a blank line (as evidenced by the IF NOT DATALINE$="" test), then the line is skipped. If the line is good, it is PRINTed to the standard output along with whatever it is we want added to the front of the line. Using PRINT like this (instead of having a dedicated output file) lets us either redirect it into a file (which I did with the > OUTPUT.BAT line) or pipe it through more filtering. Finally, SYSTEM will end the running Basic program and exit QBasic, thereby returning control to our batch file. You'll need to write a PROCESS.BAT which will process each line one-at-a-time. 


If you want flexible, try input and output piping with this QBASIC batch program:

echo+ON ERROR GOTO done > prepend.bas
echo DO >> prepend.bas
echo   LINE INPUT "call process.bat ", dataline$ >> prepend.bas
echo LOOP >> prepend.bas
echo done: >> prepend.bas
echo SYSTEM >> prepend.bas
qbasic /run prepend.bas < input.txt > output.bat
del prepend.bas
The only real difference is the LINE INPUT. Without a file number specified, it reads lines from "Standard Input". This can be either the keyboard or a redirected or piped input. But telling where the end of the piped input is becomes a problem! It just keeps reading until it crashes with an "Input Past End of File" error. So we simply use ON ERROR to trap that error and use it to end the program. A side effect of not being able to see the end until you're past it is that this method puts an extra CALL PROCESS.BAT at the end of your output file. You'll have to make sure whatever your PROCESS.BAT does, it ignores being called with no arguments. If you read this far, you deserve to know about the plus sign on the first line.

Lost? Look at the site map.

Bad links? Questions? Send me mail.

Google
Yahoo
Ask Jeeves