1.

Solve : How to insert a line of code into multiple xhtml files?

Answer»

My troubles began when I created hundreds of xhtml files for several epub ebooks and didn't place certain code into them.
So now I am looking for a way to insert a line of code into all these xhtml files if one swoop, without manually opening each one and pasting the code.

The code needs to be placed within the tags. since the xhtml files can easily be converted to simple txt files, and then converted back I thought/think it would not be really toooooo difficult.

The code strings I'd like to insert are like the following:
Code: [Select]<style>
img {
align:middle;
max-width: 95%;
height: auto;
}
</style>
which can easily be rendered as:
Code: [Select]<style>img{align:middle;max-width:95%;height:auto;}</style>
The other code string is:
Code: [Select]<link rel="stylesheet" type="text/css" href="styles.css" />
Note: All xhtml files are located in a single directory, a good thing!

Would be most appreciative for some help.

Brian


This uses a native Windows batch script called Jrepl.bat written by dbenham, which uses jscript to make it robust and swift.
http://www.dostips.com/forum/viewtopic.php?f=3&t=6044

Place Jrepl.bat in the same folder as the batch file, or in a folder that is on the system path.


There is also copy on Dropbox (unblock it after downloading):
https://www.dropbox.com/s/4otci4d4s8x5ni4/Jrepl.bat





Code: [Select]@echo off
for /f "delims=" %%a in ('dir *.xhtml /b /a-d ') do (
echo processing "%%a"
call jrepl "<head>" "<head><style>img{align:middle;max-width:95%%%%;height:auto;}</style><link rel=\qstylesheet\q type=\qtext/css\q href=\qstyles.css\q />" /L /x /f "%%a" /o -
)
pause
Hello Foxidrive

Not to hijack this in any way but curious..

I was following this one because it caught my interest to see what becomes of it. I can see this beneficial in many ways other than xhtml and was wondering what would be required to insert information between 2 lines as its own line such as if you can add a line count to specify for it to insert on the 8th line no matter what information was proceeding etc?

In the past I have had to WRITE a program that passes text from the original file to the new temp file and stop writing to the new temp file at a string or character flag, or even character count to reach and then append information that needed to be added in, and then follow through picking up where it left off at the character count to append the information from the original file to the new temp file to have the information added in the temp file, then delete the original file but then rename the temp file as the original file name.

So I guess the question is ... is there a way to specify which line to write information to, to shove the information at that line number, and all other lines below it down a line for the new information to be added using batch without looking for a string, character, or character count as the trigger to when to add information?Quote from: foxidrive on February 26, 2016, 02:17:03 AM

This uses a native Windows batch script called Jrepl.bat written by dbenham, which uses jscript to make it robust and swift.
http://www.dostips.com/forum/viewtopic.php?f=3&t=6044

Place Jrepl.bat in the same folder as the batch file, or in a folder that is on the system path.



Code: [Select]@echo off
for /f "delims=" %%a in ('dir *.xhtml /b /a-d ') do (
echo processing "%%a"
call jrepl "<head>" "<head><style>img{align:middle;max-width:95%%%%;height:auto;}</style><link rel=\qstylesheet\q type=\qtext/css\q href=\qstyles.css\q />" /L /x /f "%%a" /o -
)
pause

@foxdrive,
Thanks for the code help. Thought I have not tried it yet, I am hoping it will do the trick.

Q: the section of code you wrote contains:

Code: [Select]call jrepl "<head>" "<head>...
normally in html the tags are:
Code: [Select]<head></head>...
In other words the closing head tag includes a backslash. Should your code include this or am i interpreting it wrong?
Thanks!

UPDATE:
Well, I ran the code on three dummy xhtml files and it did exactly as asked for. I guess I just do not understand the code you wrote as well as I thought.

Can I assume that these bath files would allow me to insert a single line of text/code after any specified html tag, or text string.
Example: Code: [Select]<div> or <p> or even the word string "in the end"
Also, DaveLembke's question is most welcome here as well. Ya never know when you might need that as well.

I suppose that reading about the Jrepl.bat and it's possibilities would be helpful. It appears to be quite extensive. WOW! Nice code. I just executed it on more than 100 files and it took less than a minute, something that would have taken me "hours?" to do manually.

Really appreciate the help...foxidrive's the Man...Thank you patio and guys, you're kind.

Regarding Quote from: DaveLembke on February 26, 2016, 06:25:13 AM
is there a way to specify which line to write information to, to shove the information at that line number, and all other lines below it down a line for the new information to be added using batch without looking for a string, character, or character count as the trigger to when to add information?

There's another great tool by aacini which is also based on jscript:

Using findrepl.bat this task could be achieved as this sample shows:

findrepl /O:S:E <"file.txt" in this syntax redirects the file and Outputting from line Start to line End

If End is omitted then it continues to the end of the file.

Code: [Select]@echo off
set "file=sample.txt"

echo ======================
(
echo one wife
echo two girlfriends
echo three beers
echo four apples
echo five pears
echo SIX shooter
echo seven samurai
echo eight harley davidson's
) >"%file%"
type "%file%"
echo ======================

REM The code above just creates the sample text file.
REM The code below takes lines 1 to 5, adds a new line 6, and then takes line 6 to the end of the file

(
call findrepl /o:1:5 <"%file%"
echo new line six
call findrepl /o:6 <"%file%"
)>"%file%.2"
move /y "%file%.2" "%file%" >nul

type "%file%"
echo ======================
pause
del "%file%"



This uses a native Windows batch script called Findrepl.bat written by Aacini
http://www.dostips.com/forum/viewtopic.php?f=3&t=4697

Place Findrepl.bat in the same folder as the batch file, or in a folder that is on the system path.

There is also copy on Dropbox (unblock it after downloading):
https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat


======================
one wife
two girlfriends
three beers
four apples
five pears
six shooter
seven samurai
eight harley davidson's
======================
one wife
two girlfriends
three beers
four apples
five pears
new line six
six shooter
seven samurai
eight harley davidson's
======================
Press any key to continue . . .Sweet! Thanks Foxidrive for your additional effort on this one. Saving this info for fact that I know I will be using it in the future. In the past I had to manually go into files requiring info added at a specific (line) placeholder within the file when there was no constant to look for among the files. Hopefully some others find this helpful too.

Example of this in some machinery that has config files and many of them, and adding an additional step in the text file ladder logic for PLC etc

Move x=123.45
Move y=23.456
Pause = 3000ms
Move x=-123.45
Move y=-23.456

and you want something else done in between but yet get back to the home position. Such as Z axis punching metal at a certain depth punch from its reference or home location. Below Z axis info can be added with the solution you shared no matter what information is on line 2 for Y axis and the Pause = 3000ms delimiter method isnt always there either to hunt for ms and write a line after that.

Move x=123.45
Move y=23.456
Pause = 3000ms
Move z= 53.20
Move z=-53.20
Pause = 3000ms
Move x=-123.45
Move y=-23.456

Because the values for the 2nd line for example will be different, but the Z axis punch will always punch metal at same depth just different locations, prior to you sharing your solution I had to manually go into each and every file and edit it. The machinery an engineering nightmare botched together did not come with a nice GUI or even DOS based config file job editor so you actually freely move the axis by hand onto position and measure its placement and then write down the encoder values for XYZ in which you enter this information into config files, and monitor the encoder information with software you can see real time encoder tic positions for all 3 axis for a machine that punches or stamps metal depending on depth of Z axis.

Quote from: DaveLembke on February 27, 2016, 05:40:01 AM
Sweet! Thanks Foxidrive for your additional effort on this one.

I'm glad that it's handy for your computer controlled gear.

Aacini (Antonio) and Dave Benham have really changed the way I approach batch scripts, with all their handy scripts and utilities - so useful.

A mate has a company doing laser etching, all modern stuff to me, and the staple for many years of the business has been doing engraving with older machines - some of them are controlled by Apple 2+ PCs.

Your TASKS seem similar in nature with metalwork. :thumbsup:

When it comes to machines, the computers that went with them generally get used far BEYOND the normal home or business computing use. When they are slow in comparison to newer computers, for a machine its the perfect pairing of hardware and software. Sometimes your even stuck with some machines that have ISA control cards etc having to stick to a computer for the machine that has an ISA slot, and even you might even have to stick with a specific CPU or CPU speed so that execution is not too fast for example in which a machine run initially on a 386 DX 33Mhz CPU with a 486 DX 66Mhz might execute too much faster and the machine might thrash itself to death at 2X the clock as before + a CPU that is faster due to core design of coming to a solution to its execution etc. I have had to shop ebay before for specific legacy computers before to pair a machine up with a healthy computer of same or very similar spec to avoid this problem.


Discussion

No Comment Found