html - 创建批处理/可视化基本脚本以将随机引用放入 html 文件中

标签 html vbscript batch-file jscript

在工作中,我们的最终用户使用 Windows XP 并使用 Outlook Express。每当用户撰写或回复电子邮件时,Outlook Express 都会“读取”位于 c:\的静态 html 文件,并将内容用作签名。这工作得很好。

现在我的同事给了我一个包含 100 多行的简单文本 (.txt) 文件,每行包含一个“励志名言”。

我的目标是以某种方式从这个文本文件中提取随机引用,并将其插入到静态 html 签名文件中。

由于我受限于 XP 本身支持的内容并且无法安装任何其他软件(例如 python),因此我认为批处理或 vbscript 将是正确的选择(如果不是唯一的话)。我想象一个通过执行的脚本。 Windows 任务计划程序每 15 分钟左右从 .txt 文件中随机读取一行,并将其更新到静态 html 签名文件中。

这是可能的吗,或者批处理和 vbscript 都不能做这样的事情?

任何帮助或建议将不胜感激:)

最佳答案

您可以创建一个签名模板,其中包含被延迟扩展替换的嵌入变量。任何感叹号 ! 或插入符 ^ 文字也必须编码为变量:

!QUOTE! = 随机引用
!X! = 感叹号文字
!C! = 插入符号文字(可能不需要)

可以根据需要将其他变量添加到模板中。

这里有一个简单的 HTML 模板作为例子

<!X!doctype html>
<html>
  <head>
    <title>Random Quote</title>
  </head>
  <body>
    <p><strong>!QUOTE!</strong></p>
  </body>
</html>

下面的批处理文件会从报价文件中随机选择一个报价,替换模板中的变量后写出签名文件。

编辑 - 我通过使用 FOR/F 而不是 SET/P 来读取引号行,从而提高了性能并略微改变了限制。

@echo off
setlocal disableDelayedExpansion

::Define the files
set quoteFile="quotes.txt"
set signatureTemplate="template.txt"
set signatureFile="signature.html"

::Define constants for ! and ^ substitutions in template
set "X=!"
set "C=^"

::Count the number of quotes
for /f %%N in ('find /c /v "" ^<%quoteFile%') do set quoteCount=%%N

::Pick a random number of quotes to skip
set /a "skip=%random% %% %quoteCount%"

::Load the selected quote into a variable
if %skip% gtr 0 (set skip=skip=%skip%) else (set skip=)
for /f "usebackq %skip% delims=" %%A in (%quoteFile%) do (
  set quote=%%A
  goto :break
)
:break

::Read the signature template and write the signature file
::Delayed expansion will automatically replace !quote!, !X! and !C!
setlocal enableDelayedExpansion
>%signatureFile% (
  for /f "usebackq delims=" %%A in (%signatureTemplate%) do echo %%A
)

编写的脚本有一些限制:

  • 空白或以 ; 开头的模板行将被跳过
  • 引号文件不得有任何空行或以;开头的行

关于html - 创建批处理/可视化基本脚本以将随机引用放入 html 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938818/

相关文章:

javascript - HTML/HTA 文档中鼠标位置的实时更新

mysql - SQL 不会插入到表中

windows - "was unexpected at this time."

html - 哪个是正确的方法,inline-block 还是 float?

javascript - 使用 php 请求和读取任何文件类型

javascript - 如何禁用按钮/选项卡,直到满足条件

excel - 仅将 Excel 中的可见单元格保存为 CSV

batch-file - 管道输出到删除命令

windows - 需要从 32 位和 64 位机器 Windows 机器读取注册表值

html - 如何获得三个按钮(1x 左和 2x 右)然后在下面更多?