Add to text file through a .bat

June 26, 2012 at 00:15:17
Specs: Windows XP
Add to text file through a .bat

hi I would like to add a line of text in certain position in a text file so, for example:

text file:
[subject1]
hello
welcome
etc
[subject2]
hi
bye
etc

suppose I want to enter another line under [subject2] how do I use dos command to accomplish that???

I saw the command in this forum which uses echo >> filename which is great but how do I tell it which line???


See More: Add to text file through a .bat

Report •


#1
June 26, 2012 at 06:55:12
I believe that "dos" has to rewrite the file. The echo >> apends it to the end of a file. There isn't a single command to write into a text file.

this is a simple example

@echo off
::prompts for initial string to add a line after
set /p search1="enter exact search string: "
set /p addon="please enter addition: "

::this statement pulls sample.txt line by line and looks for the %search1%
::and then "adds in" the %addon%

for /f "tokens=*" %%G IN (sample.txt) DO echo %%G>>sample2.txt && if %search1% EQU %%G echo %addon%>>sample2.txt

cls

::displays contents of replacement
type sample2.txt
::if you want to replace origional remove the double colon
::ren sample2.txt sample1.txt

:: mike


Report •
Related Solutions


Ask Question