Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would a date in HTML file using a .bat script

I have a .html file titlepage.html that contains the following line:

<span class="titlepage modified">Modified: Feb 13, 2020 </span>

The problem is, that sometimes I forget to change the "Modified: " date

I run a .bat file which moves this titlepage.html to the proper location, so I figured it would make sense to add a subroutine in the .bat file to update the date before moving it.

my current .bat file looks like:

@echo off
setlocal enabledelayedexpansion

set infile=titlepage.html
set outfile=result.html
Set find=Modified:
set replace="<span class="titlepage modified">Modified: %DATE% </span>"

del result.html

for /F "tokens=1,2 delims=" %%n in (!infile!) do (
set LINE=%%n
set TMPR=!LINE:%find%=%replace%!
echo !TMPR!>>%outfile%
)

And the line in question of my looks like: <span class="titlepage modified">"<span class="titlepage modified">Modified: 04/10/20 </span>" Feb 13, 2020 </span>

So it successfully inputs the information I want, but I need it to replace the entire line in question not just the part I used for the lookup.

*Note the dates will be arbitrary so I can't perform a lookup on the entire line.

So I want to: 1. Lookup for a line containing a string 2. Replace that entire line with my new input.

Thanks in advance.

like image 713
Max O Avatar asked Sep 10 '26 15:09

Max O


1 Answers

@echo off
setlocal enabledelayedexpansion

set "target=./titlepage.html"
set "destination=./titlepage.html"
set "find=(?mi)^.*Modified:.*$"
set "replace=<span class=\"titlepage modified\">Modified: %DATE% </span>"

powershell -noprofile -command^
 $content=get-content -path '!target!';^
 $result=$content -replace '!find!', '!replace!';^
 set-content -path '!destination!' -value $result

powershell reads the the target ./titlepage.html, does the replacement and writes to the destination ./titlepage.html.

The regular expression pattern searches for Modified: in a line. The find variable contains the pattern and uses options (?mi). The m is multiline mode and the i is case insensitive. If you want case insensitive, remove the i.

Regular expression pattern:

  • (?mi) Regular expression options. m is multiline and i is case insensitive.
  • ^ By default, the match must start at the beginning of the string; in multiline mode, it must start at the beginning of the line.
  • . Wildcard: Matches any single character except \n.
  • * Matches the previous element zero or more times.
  • $ By default, the match must occur at the end of the string or before \n at the end of the string; in multiline mode, it must occur before the end of the line or before \n at the end of the line.

Replacement string:

  • \" Escape nested double quote for command line parsing to powershell.

View the Regular Expression Language - Quick Reference for more information.

The ^ at the end of lines, of the powershell command, are line continuations.

like image 184
michael_heath Avatar answered Sep 13 '26 06:09

michael_heath



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!