copying part of a file to another

April 16, 2009 at 12:10:55
Specs: Windows XP
I want to use a batch file to copy part of a file to another file. Specifically from html to html. The section i want to copy is surrounded by comment <!--menu-->. I want to copy from one and put that code in other html files replacing what was in the comments. Any help would be greatly appreciated.

Basically this is what it would look like

file1.html
code
code
<!--menu-->
code needed to be copied
<!--menu-->
code

file2.html
code
<!--menu-->
codeneeded to be replaced
<!--menu-->
code


See More: copying part of a file to another

Report •


#1
April 17, 2009 at 04:50:02
@echo off > file2.html & setLocal EnableDelayedExpansion

set start=
set end=
set /a N=0

for /f "tokens=* delims=" %%a in (file1.html) do (
set /a N+=1
if "%%a" equ "<!--menu-->" (
if defined start (
set end=!N!
) else (
set start=!N!
)
)
)

set /a N=0

for /f "tokens=* delims=" %%a in (file1.html) do (
set /a N+=1
if !N! geq !start! if !N! leq !end! (
echo %%a >> file2.html
)
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
April 17, 2009 at 07:05:17
I probably wasn't clear before and thanks for your efforts but that file didn't seem to do what I needed. I was looking to copy what was in file 1 between <!--menu--> comments and paste that over existing code that is between <!--menu--> comments on multiple pages. Thank you for your post though and the quick response. Sorry I wasn't more clear. If you have any other ideas that would be greatly appreciated.

Report •

Related Solutions


Ask Question