Good to see the batch language is alive and well!
I am trying to write a batch file that uses edlin to change the contents of a
text file. I've been able to do this successfully in the past using insert
and delete commands to make simple edits. I create a "script" file and then
redirect this into edlin.
Now I'm trying to use the serach and replace command and have hit a problem.
The batch file concerned is:
if %1x==x goto usage
if not exist week%1.wkl goto noweek
copy waymast.txt way%1.txt /y
rem [C_Z] in below line is a Ctrl+Z character
echo 1,999rRRRR[C_Z]%1 > wayedit.edl
echo e >> wayedit.edl
edlin way%1.txt < wayedit.edl
if exist way%1.edl del way%1.edl
if exist way%1.bak del way%1.bak
goto end
:noweek
echo Week %1 files do not exist.
echo Have you run the weekly steps yet or are you just in the wong directory?
goto end
:usage
echo Usage is MAKEWAY [weekno]
echo [weekno] is this week's number
echo eg MAKEWAY 0001
goto end
:end
When run the batch file stops after the Ctrl+Z on the fifth line. I've
inserted this literally with my text editor. Is there a way that I can insert
this character into the "script" file without DOS thinking it marks the end
of the batch file?
I'd appreciate any suggestions or comments. Thanks.
Robert
My suggestiopn is to give up EDLIN and use an editor *designed* for
use with scripts, or else a text management oriented language. That
EDLIN works ar all with scripts is a byproduct of its use of STDIN and
STDOUT streams for input and output, it is not a feature. The
standard script controlled editor is SED (Stream EDitor), which can be
found at <ftp://ftp.simtel.net/pub/simtelnet/gnu/gnuish/> and numerous
other archives (completely free: GPL).
Personally, I use AWK for so many text related things that I just use
it for script controlled editing as well. If the only automated text
processing you do is editing, then SED is the tool of choice.
T.E.D. (tda...@gearbox.maem.umr.edu - e-mail must contain "batch" in the subject or my .sig in the body)
I use a batch which the main part of I got from this newsgroup some time
ago. As I remember, it was put together by Mark Stang, but was pretty much
a group effort. I am developing it, but have not yet gotten further than
this. Eventually, it will be able to handle special characters and multiple
words, as well as make multiple passes as needed. The way edlin works is
it uses a block of memory (I don't know how much) at a time, so if the
file is large it may take multiple passes to make all changes.
:: replac.bat
@ECHO off
IF not "%3"=="" GOTO start
:help
ECHO.
ECHO %%1 is target file
ECHO %%2 is text to replace
ECHO %%3 is replacement text
ECHO.
ECHO Examples:
ECHO replace test.txt A a : replaces A with a
ECHO replace test.txt color colour : replaces color with colour
ECHO replace test.txt Y N : replaces Y with N
ECHO.
ECHO File to be changed must be in current directory, which
ECHO must be writeable. Filename only; no colon or backslash.
ECHO.
GOTO end
:start
ECHO %1 |FIND ":" >nul
IF not errorlevel 1 ECHO colon or backslash not allowed
IF not errorlevel 1 GOTO help
ECHO %1 |FIND "\" >nul
IF not errorlevel 1 ECHO backslash or colon not allowed
IF not errorlevel 1 GOTO help
IF not exist %1 ECHO "%1" not in current directory
IF not exist %1 GOTO help
:: Create an empty script file >%temp%.\replc.scr
FOR %%v in (f100LFF''20 l102 w q) do ECHO %%v>>%temp%.\replc.scr
:: text to be replaced follows immediately after 'R' below
ECHO 1,R%2> %temp%.\part1.txt
:: the following adds two spaces to the front of part1.txt,
:: thus pushing the CR/LF off the end.
:: this works becuase EDLIN.EXE ignores leading spaces
IF exist %temp%.\part1.txt DEBUG.EXE %temp%.\part1.txt <%temp%.\replc.scr> nul
DEL %temp%.\replc.scr > nul
IF exist %temp%.\ctrlz.dat DEL %temp%.\ctrlz.dat > nul
:: this creates a 1 byte file with Ctrl-Z in it
ECHO E| EDLIN.EXE /B %temp%.\ctrlz.dat>nul
IF exist %temp%.\part2.txt DEL %temp%.\part2.txt
:: replacement text follows after 'ECHO' on next line
ECHO %3>%temp%.\part2.txt
ECHO e>>%temp%.\part2.txt
COPY /B %temp%.\part1.txt+%temp%.\ctrlz.dat+%temp%.\part2.txt %temp%.\edscr.txt >nul
EDLIN.EXE %1 <%temp%.\edscr.txt> nul
:: This deletes the EOF character which was added at the end
COPY/B/Y %1/A+,, . %Place a '>nul' here to supress screen output%
DEL %temp%.\ctrlz.dat
DEL %temp%.\edscr.txt
DEL %temp%.\part?.txt
:end
I use replac.bat to search and replace words in ASCII files. I have
another copy called replc.bat which I use when I want to replace more
than one word at a time or use special characters in which case I just
edit the batch and 'hard wire' the two strings into their designated
places. This can be an amazing time saver in changing multiple files
such as web pages, for example. If you have an url to change on many
pages, it can be done quickly right at the prompt.
for %f in (*.htm) do call replc %f
Changes made on all pages.
See also here:
http://fbox.vt.edu:10021/A/alrobin2/home/edlin/index.html
Edlin - Main Page; Use edlin on any MS-DOS version without setver.exe
http://www.wwwebspace.co.uk/~iskra/edlin/
Welcome to the Friendly Edlin Page!
I hope this helps.
--
Outsider
MS-DOS 6.22, Windows for Workgroups 3.11, Netscape Navigator 4.08
I have the simplest tastes. I am always satisfied with the best.
- Oscar Wilde
<snip>
> I'd appreciate any suggestions or comments. Thanks.
>
> Robert
Here is a QBasic solution. Run it as you would your own batch.
:: QBway.bat
@echo off
if %1x==x goto usage
if not exist week%1.wkl goto noweek
echo> QBway.bas open "waymast.txt" for input as #1
echo>>QBway.bas open "way%1.txt" for output as #2
echo>>QBway.bas do while not eof(1)
echo>>QBway.bas line input #1,text$
echo>>QBway.bas do
echo>>QBway.bas x=instr(text$,"RRRR")
echo>>QBway.bas if x then mid$(text$,x,4)="%1"
echo>>QBway.bas loop until x=0
echo>>QBway.bas print #2, text$
echo>>QBway.bas loop
echo>>QBway.bas system
QBasic /run QBway.bas
del QBway.bas
goto end
:noweek
echo Week %1 files do not exist.
echo Have you run the weekly steps yet or are you just in the wrong directory?