Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a batch/visual basic script to put a random quote into an html file

At work our end-users are on Windows XP and using Outlook Express. Whenever a user composes an email or replies to one, Outlook Express "reads" a static html file located on c:\, and uses the content as a signature. This works perfectly fine.

Now my coworker gave me a simple text(.txt) file with 100+ lines, each line containing a "motivational quote".

My objective is to somehow have a random quote extracted from this text file, and inserted into the static html-signature file.

Since I am limited to what XP natively supports and can't install any additional software such as python, I assume either batch or vbscript would be the proper choice (if not only). I imagine a script which is executed via. the Windows Task Scheduler every 15 minutes or so, which randomly reads a line from the .txt-file, and updates it into the static html-signature file.

Is this possible in any way, or are neither batch nor vbscript capable of doing something like that?

Any help or advice will be GREATLY appreciated :)

like image 775
Amivit Avatar asked Dec 28 '25 21:12

Amivit


1 Answers

You can create a signature template that has embedded variables that are replaced by delayed expansion. Any exclamation point ! or caret ^ literals must be encoded as variables as well:

!QUOTE! = The random quote
!X! = exclamation point literal
!C! = caret literal (probably not needed)

Additional variables could be added to the template as needed.

Here is a trivial HTML template as an example

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

The following batch file will select a random quote from the quote file and write out the signature file after replacing the variables in the template.

EDIT - I improved performance and slightly altered the limitations by using FOR /F to read the quote line instead of 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
)

There are a few limitations to the script as written:

  • Template lines that are blank or begin with ; will be skipped
  • The quotes file must not have any blank lines or lines that start with ;
like image 68
dbenham Avatar answered Dec 31 '25 12:12

dbenham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!