使用 cmd/batch 文件将文本字符串添加到 html 文档中

Add a string of text into a html document using cmd/batch file

我想将一串文本添加到 html 文档中。
我的网站有一个 "update" 部分,您可以在其中看到他们开车去了哪里。 (它是一些骑自行车的人的网站)
我想这样做,所以我爸爸只需要打开一个 bat 文件,输入一些信息,然后它将字符串添加到 html 文档中。
我目前使用 "table" 作为布局。
这是代码:

 <tr style="mso-yfti-irow:34">
    <td valign="top" style="width:180;padding-left:3.5pt; padding-light:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
        <font size="4">DATE</font>
    </td>
    <td valign="top" style="width:500;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
         <font size="4">LOCATION</font></td>
    <td valign="top" style="width:523;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
        <font size="4">AMOUNT</font>
    </td>
 </tr>


当前网站

<html>
    <head>
        Some things here....
    </head>
    <body>
        <div>
            More things here
        </div>
        <table>
            the table/the place where new text should be added
        </table>
        A bit more
    </body>
 </html>    


我的意思是我可以用这段代码制作一个 bat 文件

echo off
set /p Date="Date: "
set /p Location="Location: "
set /p Amount="Amount: "

(some command here to add it)

<tr style="mso-yfti-irow:34">
    <td valign="top" style="width:180;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
    <font size="4">%Date%</font></td>
    <td valign="top" style="width:500;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
    <font size="4">%Location%</font></td>
    <td valign="top" style="width:523;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
    <font size="4">%Amount%</font></td>
</tr>

Exit


新文本应始终添加到 table 的底部,而不是 html 文档的底部
线路号。也会一直变
我希望我解释得足够好,如果有什么不明白的就写,然后我会尽力澄清

听起来您已经编写了 bat 脚本来提示您父亲输入值。因此,我将重点介绍如何将它们插入您的 html 页面。

在您当前的 html 文件中,更改 table 末尾的格式,将最后一个 直接放在

</tr></table>

在你的 bat 文件中,替换

(some command here to add it)
<tr style="mso-yfti-irow:34">
   <td valign="top" style="width:180;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
   <font size="4">%Date%</font></td>
   <td valign="top" style="width:500;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
   <font size="4">%Location%</font></td>
   <td valign="top" style="width:523;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
   <font size="4">%Amount%</font></td>
   </tr>

有了这个

powershell -command "(Get-Content Name_of_File.html) -replace '</tr></table>', '<tr style=&quot;mso-yfti-irow:34&quot;> <td valign=&quot;top&quot; style=&quot;width:180;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm&quot; height=&quot;5&quot;><font size=&quot;4&quot;>%Date%</font></td><td valign=&quot;top&quot; style=&quot;width:500;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm&quot; height=&quot;5&quot;><font size=&quot;4&quot;>%Location%</font></td><td valign=&quot;top&quot; style=&quot;width:523;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm&quot; height=&quot;5&quot;><font size=&quot;4&quot;>%Amount%</font></td></tr></table>' | Set-Content Name_of_File.html"
Powershell.exe -executionpolicy remotesigned -File replace_quot.ps1

这将从批处理脚本中执行 PowerShell 搜索和替换命令以查找 并将其替换为新行。每次你爸爸在页面上添加新的行程时,它都会将信息添加到 table.

的底部

powershell 命令 让批处理知道在 "double quotes" 之间作为 powershell 执行所有操作。 get-content 读取您的 html 文件。因此,将 Name_of_File.html 替换为您的 HTML 页面的名称。

命令的下一部分执行搜索和替换。您必须使用“"而不是“让 powershell 将整行读取为一组流畅的操作。它将编写 HTML 代码并使用你的 %Date%、%Location% 和 %Amount% 变量作为新值。

然后使用 set-content 将文件写回到 html 页面。因此,请确保 Name_of_File.html 已在此处更新为 HTML 页面的名称。

将以下行保存在名为 replace_quot.ps1 的不同脚本中,并将此脚本放在与 bat 文件相同的目录中。

(Get-Content Name_of_File.html) -replace '&quot;', '"' | set-content Name_of_File.html

您的 bat 文件将调用此脚本来替换 &qout;使用 html 文件中的实际 " 字符。