General Community > Scripting Help

Implement header in 100's of HTML files all in one directory

(1/2) > >>

colby2152:
I have a program installed on my website.  Per manual uploads of a "file", all of the 100's of HTML pages update from the offline engine.  I want to force an include statement at the top of every single page at all times.  Is there any way to do this with a local .htaccess file or some other means?  Essentially, this action would need to be applied to all of the files under a given directory. 

MrPhil:
You're starting with hundreds of .html files in a directory. You want to add a line to each file?

First, there's no point in adding an include statement to an HTML file. It won't be processed on the server. You would either need SHTML, or a scripting language like PHP. You could add the final HTML code line(s) to an HTML file, presumably somewhere inside the page.

If you want to add something at the very beginning, and you're on a Linux server,

--- Code: ---for file in `*.html`; do
  cat new_lines $file > tempfile
  mv tempfile $file
done
--- End code ---
would be the starting point. Something similar could be done for sticking new lines on to the end. You want to make sure you hit only the .html files, and this assumes that all of them need updating. There should be similar capability for Windows .bat (looping through all files *.html), with copy new_lines+$file tempfile or something like that. I don't use complex Windows batch files so I'm no authority on that.

To insert lines in the middle of an .html file, you might use the "sed" utility to match a certain place (e.g., <body> and replace it by <body>new lines.

Are the files refreshed each time you perform this operation, or do you need to differentiate between old files that have already been modified and new ones that need modification? If the latter, you would have to look for some marker that indicates that the modification has already been done, and skip those files.

Yoshi:
Colby, did you get this solved?

colby2152:

--- Quote from: Yoshi2889 on April 22, 2012, 04:18:26 PM ---Colby, did you get this solved?

--- End quote ---

Not yet, but just reviewing the reply.


--- Code: ---for file in `*.html`; do
  cat new_lines $file > tempfile
  mv tempfile $file
done
--- End code ---

So, if I want to insert some HTML at the beginning, as well as the end, of a bunch of HTML files in a directory, then I need to create a *.bat file in that directory with the above code?  It sounds like a solution may be building, but I am unclear as to the steps.  Do you have a 'Hello World' example that can be inserted at the beginning of all HTML pages and the directory www.example.com/testfiles/?

MrPhil:
Whoa there. That code snippet is for a Linux machine (as most servers are). It's not a Windows batch file, it's a Linux shell script. You need to find out how to create and run shell scripts on your server command line (assuming you're on Linux).

First of all, what do the current HTML files look like, and what are you proposing to add to the beginning and end? If they already start with <HTML> and end with </HTML> you're likely to just make a mess if you glue on stuff at the beginning and end. What kind of changes are you proposing to make?

Navigation

[0] Message Index

[#] Next page

Go to full version