Solved Edit notepad using batch file

October 21, 2011 at 04:54:01
Specs: Windows XP
I want to add some text to 100 lines of a notepad. For example

I have a file called d.txt and want to add test to all 100 lines of d.txt.

The output should be

test line 1
test line 2
test line 3

I am aware of dos command and batch utility but not sure as how do I do it.

Some one please help me.


See More: Edit notepad using batch file

Report •


✔ Best Answer
October 24, 2011 at 12:22:19
@echo off
set local
REM looping through d.txt evaluating it line by line
for /f "tokens=*" %%a in (d.txt) do (
REM writing to d_new.txt the string "test", the value of the current line from d.txt, and the string "similar_kind_of_text"
echo test %%a similar_kind_of_text >> d_new.txt
)
REM after loop is finished, deleting original d.txt
del /q d.txt
REM renaming d_new.txt to d.txt
ren d_new.txt d.txt
endlocal


#1
October 22, 2011 at 00:33:54
@echo off

set incr=0
:write
set /a incr=%incr%+1
if %incr% geq 101 goto end
echo test line %incr% >> d.txt
goto write
:end


Report •

#2
October 22, 2011 at 02:39:33
Thanks a lot for the reply. But since I am not much into programming I am not sure as what word will this code it will insert into the test file and if I need to insert some other text at the end of line how do I do that.

Expecting some reply and thanks a lot for hte solutions.


Report •

#3
October 22, 2011 at 02:42:37
Also how do I run this code. Should I make a bat file or how do I run this code.

Report •

Related Solutions

#4
October 22, 2011 at 13:36:16
Make a batch file.
yourtexthere.bat

Report •

#5
October 23, 2011 at 23:05:12
Thanks for the replies. I am able to run using the batch file. But I think I made a mistake in putting my question. Hence trying to reframe the question properly. I want to insert some text ahead of a text file. The text file is having some data in a columnar format for e.x.

100
400
500
600
700
900

The above nos are just an example. It could be even data in form of words such as hi
how
hello

Hence I want the data to appear as

test 100
test 400
test 500
test 600
test 700
test 900

testhi
test how
test hello

So kindly help me as how do I this.


Report •

#6
October 24, 2011 at 08:26:21
So the text file already has a value on each line. That means we don't need to increment a number or anything like that, we just need to add "test" to the start of each line.

@echo off
set local
for /f "tokens=*" %%a in (d.txt) do (
echo test %%a >> d_new.txt
)
del /q d.txt
ren d_new.txt d.txt
endlocal


Report •

#7
October 24, 2011 at 08:41:37
Thanks a ton. Its working. Now I have small problem or query. I need to some similar kind of text at the end of each line. So how do I add that text. If possible try to add the code in the above answer only so I have to work with just 1 batch file and also guide me as what each code is doing, so I can know accordingly.

Report •

#8
October 24, 2011 at 12:22:19
✔ Best Answer
@echo off
set local
REM looping through d.txt evaluating it line by line
for /f "tokens=*" %%a in (d.txt) do (
REM writing to d_new.txt the string "test", the value of the current line from d.txt, and the string "similar_kind_of_text"
echo test %%a similar_kind_of_text >> d_new.txt
)
REM after loop is finished, deleting original d.txt
del /q d.txt
REM renaming d_new.txt to d.txt
ren d_new.txt d.txt
endlocal

Report •


Ask Question