Batch file to add line to top of data

Microsoft Windows xp professional w/serv...
February 12, 2010 at 07:05:56
Specs: Windows XP, Centrino 2 vPro
Hello!

Using batch commands, I'd like to know how to add line to top and bottom of data in a file. Thanks!!!


For example, the original data is:

12345
54321
22222
33333

And I'd like it to look like this:


***New top line***

12345
54321
22222
33333

***New bottom line***


See More: Batch file to add line to top of data

Report •


#1
February 12, 2010 at 07:13:45
@echo off
echo ***New top line*** > temp.txt
for /f "tokens=*" %%a in (yourfile.txt) do (
  echo %%a >> temp.txt)
echo ***New bottom line*** >> temp.txt
echo y|del yourfile.txt
rename tempt.txt yourfile.txt 

Not tested but should work. Backup up your old file before testing on it of course.


Report •

#2
February 12, 2010 at 07:32:54
Slightly shorter:

----------------------------
@echo off
echo ***New top line*** > temp.txt
type myfile >> temp.txt
echo ***New bottom line*** >> temp.txt
move /y temp.txt myfile


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

M2


Report •

#3
February 12, 2010 at 10:47:37
Thanks guys!!!

Report •

Related Solutions

#4
February 13, 2010 at 07:31:46
> @echo off
> echo ***New top line*** > temp.txt
> for /f "tokens=*" %%a in (yourfile.txt) do (
> echo %%a >> temp.txt)
> echo ***New bottom line*** >> temp.txt
> echo y|del yourfile.txt
> rename tempt.txt yourfile.txt
>

type /?
del /?


Report •

#5
April 15, 2010 at 11:43:35
Maybe a bit longer, but could perform better depending on the size of the file you are adding the top/bottom line to. COPY should outperform TYPE.

@echo off
echo ***New top line*** > top.tmp
echo ***New bottom line*** > bottom.tmp
copy /a top.tmp + myfile.txt + bottom.tmp myfile.tmp > nul
move /y myfile.tmp myfile.txt
del top.tmp
del bottom.tmp


Report •


Ask Question