LoginSignup
0
0

More than 3 years have passed since last update.

HTMLのheaderとfooterを挿入するVB Script

Last updated at Posted at 2021-04-10

概要

複数あるHTMLファイルにheader部分とfooter部分を挿入してくれるVB Scriptです。

経緯

phpなどで作成しているならなんら不要なVBSです…
AWS(Amazon Web Service)のS3ストレージにホームページを作成し
置く仕事をしております。ホームページの運用費が非常に安くなります。

通常サーバー運用費3000~5000円ですが約500円でできている実績です。
※あまりにも大きなファイルとかになりますと金額に差が出ますので参考までにお願いします。
別途ドメイン費用などは掛かります。

方法

  1. insert.vbsを作成するhtmlファイルと同じフォルダ(ディレクトリ)に配置してください。
  2. htmlで共通となるheader部分のheader.htmlファイルを作成してください。
  3. htmlで共通となるfooter部分のfooter.htmlファイルを作成してください。
  4. 各種コンテンツとなるhtmlファイルは後ろを_before.htmlファイルとして作成してください。 ヘッダー挿入部分に 「・・insert_header」と書き込んでください。 フッター挿入部分に 「・・insert_footer」と書き込んでください。

例:3ページありましたら
index_before.html
hogehoge1_before.html
hogehoge2_before.html
と言った感じに作成してください。
hogehoge部分は各自作成したファイルでよいので名前のhogehogeやindexはあくまで例です。

insert.vbs ソース

insert.vbs

Option Explicit

Dim FSO,oFolder,oFile,strFolder,oADO_Read,oADO_Write,oADO_ReadBefore

Dim oInsert_Headertext,oInsert_Footertext
Dim strHeaderText,strFooterText,strHTML,strNewFile
Dim bln_INsert

Set FSO = CreateObject("Scripting.FileSystemObject")

'各種ファイルの読み込み専用ADO(UTF-8で読み込みを行う。)
Set oADO_Read = CreateObject("ADODB.Stream")
With oADO_Read
    .Charset = "UTF-8"
    .Open
End With

Set oADO_ReadBefore = CreateObject("ADODB.Stream")

'_beforeファイルを開くためのADO(UTF-8で読み込みを行う。)
With oADO_ReadBefore
    .Charset = "UTF-8"
End With



'ファイルの書き込み専用ADO(UTF-8で読み込みを行う。)
Set oADO_Write = CreateObject("ADODB.Stream")
With oADO_Write
    .Charset = "UTF-8"
End With

'このinsert.vbsファイルがある場所を取得しておきます。
strFolder = FSO.getParentFolderName(WScript.ScriptFullName) & "/"

'FSOでフォルダーオブジェクトを設定。
set oFolder = FSO.getFolder(strFolder)'フォルダーオブジェクト取得


'header.htmlとfooter.htmlのファイルパス
strHeaderText = strFolder & "header.html"
strFooterText = strFolder & "footer.html"

'各ファイルの存在チェック
If FSO.FileExists(strHeaderText) = False Then
    MsgBox strHeaderText & "が存在しません。"
    WScript.Quit
End If

If FSO.FileExists(strFooterText) = False Then
    MsgBox strFooterText & "が存在しません。"
    WScript.Quit
End If



'ヘッダーを読み込んで変数に格納
oADO_Read.LoadFromFile strHeaderText
strHeaderText = oADO_Read.ReadText

'フッターを読み込んで変数に格納
oADO_Read.LoadFromFile strFooterText
strFooterText = oADO_Read.ReadText

oADO_Read.Close'oADO_Readは閉じておく

'すべてのファイルでループ
For Each oFIle in oFolder.Files

    If instr(oFile.Name,"_before.html") <> 0 Then

        strNewFile = changeFileName(oFile.Name)'ファイルの名前から新規_beforeがないファイルの作成を行う。
        '編集前の内容を読み込む
        oADO_ReadBefore.Open
        oADO_Write.Open
        oADO_ReadBefore.LoadFromFile oFile.Name

        Do Until oADO_ReadBefore.EOS
            strHTML = ""
            strHTML = oADO_ReadBefore.ReadText(-2)'1行読み込む

            bln_Insert = False'ヘッダー・フッターを挿入したかのチェックフラグ

            'headerの挿入
            If instr(strHTML,"・insert_header") <> 0 Then
                bln_Insert = True'挿入したことを判断
                oADO_Write.WriteText strHeaderText,1
                oADO_Write.WriteText " ",1
            End If

            'footerの挿入
            If instr(strHTML,"・insert_footer") <> 0 Then
                bln_Insert = True'挿入したことを判断
                oADO_Write.WriteText strFooterText,1
                oADO_Write.WriteText " ",1
            End If

            If bln_Insert = False Then'インサートされていないときは通常書き込み
                oADO_Write.WriteText strHTML,1
            End If

            '最終行書き出しだったらloopを終了させる
            If instr(strHTML,"</html>") <> 0 Then
                exit do
            End If
        Loop

        oADO_Write.SaveToFile strNewFile,2
        oADO_ReadBefore.Close
        oADO_Write.Close
    End If

Next

'使ったオブジェクトをしまいましょう
Set FSO = Nothing

Set oADO_Read = Nothing
Set oADO_Write = Nothing
Set oADO_ReadBefore = Nothing

MsgBox "処理完了",64,"insert.vbs"

Function changeFileName(strFileName)
    changeFileName = Left(strFileName,Len(strFileName) - Len("_before.html")) & ".html"
    'もし_beforeが存在していたらこれから生成するので削除しておく。
    If FSO.FIleExists(changeFileName) = True Then
        FSO.DeleteFile changeFileName
    End If
End Function

FSOを使用していますが通常通り利用すると文字コードがUTF-8にならないところがポイントです。

サンプル

header.html
<!doctype html>
<html lang="ja">
<head>
<titel>サンプルページ</title>
</head>
<body>
ヘッダー書き込み開始<br>
どのページも大体&lt;head&gt;&lt;/head&gt;は一緒だよねww<br>
ヘッダー書き込みはここまで~<br>
footer.html
<footer>
フッターの書き込み開始<br>
共通フッター情報をここに書く<br>
フッターの書き込み終了<br>
</footer>
index_before.html
・insert_header
実態の内容をここに書くwww<br>
</html>

insertVBS_description.png

これらのファイルを同一フォルダに入れ、insert.vbsを実行してください。
処理完了のメッセージが表示されましたら終わりです。
同一フォルダーに、hogehoge_before.htmlではないhogehoge.htmlが作成されます。
サンプルだとindex.htmlになります。

事前に***.htmlが存在している場合はvbsで削除していて上書き状態になりますのでご注意ください。

サンプルのindex.html
sh.png

生成されたindex.html

index.html
<!doctype html>
<html lang="ja">
<head>
<titel>サンプルページ</title>
</head>
<body>
ヘッダー書き込み開始<br>
どのページも大体&lt;head&gt;&lt;/head&gt;は一緒だよねww<br>
ヘッダー書き込みはここまで~<br>

実態の内容をここに書くwww<br>

<footer>
フッターの書き込み開始<br>
共通フッター情報をここに書く<br>
フッターの書き込み終了<br>

</footer>

</html>

となります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0