Inserting text in a txt file using DOS BATCH

September 20, 2010 at 09:05:29
Specs: Windows XP
I have a txt file (say input.txt) with the following lines [as specified in BEFORE] in it. Assuming that there are two txt files 123.txt and 456.txt, I must be able to insert the contents [as specified in AFTER]

Please note that there can be multiple occurrences of "<node>some node value</node>" in a sequence and only after the last occurrence the contents of 123.txt file must be inserted. Similarly there can be multiple occurrences of "<child>some child value </child>" in a sequence and only after it's last occurence the contents of 456.txt must be inserted. Please kindly help me out with a simple sample program as how to do it.

BEFORE:input.txt

<node>some node value</node>
..........................
..........................
<node>some node value</node>
<child>some child value </child>
..........................
..........................
<child>some child value</child>

AFTER:input.txt

<node>some node value</node>
..........................
..........................
<node>some node value</node>
<!-- Insert the contents of a text file 123.txt using DOS BATCH-->
<child>some child value </child>
..........................
..........................

<child>some child value</child>
<!-- Insert the contents of a text file 456.txt using DOS BATCH-->


See More: Inserting text in a txt file using DOS BATCH

Report •


#1
September 20, 2010 at 10:05:58
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "<node>" ^< input.txt') do (
set O=%%a
)

for /f "tokens=1 delims=[]" %%a in ('find /n "<child>" ^< input.txt') do (
set T=%%a
)

set N=
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N+=1
>> newfile echo.%%a
if !N! equ !O! >> newfile type 123.txt
if !N! equ !T! >> newfile type 456.txt
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
September 21, 2010 at 01:35:02
Thank you

Report •

Related Solutions


Ask Question