Edit file in loop?
Posted: 15 May 2023 02:48
Hello,
I'd like to add a "Last updated YYYY-MM-DD" line at the bottom of all HTML files in a directory, right before </body>.
Is there a way to modifiy each file in the loop, instead of eg. saving the edit into a temporary file, and replacing the original with the temp file?
Thank you.
--
Edit: Maybe it's a bit extreme in CMD, and I'll just use an AutoIT script instead
I'd like to add a "Last updated YYYY-MM-DD" line at the bottom of all HTML files in a directory, right before </body>.
Is there a way to modifiy each file in the loop, instead of eg. saving the edit into a temporary file, and replacing the original with the temp file?
Code: Select all
@echo off
set dt=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
set dt=%dt: =0%
for %%i in (*.html) do (
REM First run: String not there yet -> force; Kludge but until I find a better way...
grep -Poha "^</body>" "%%i" | sed -r "s@^</body>@Last updated %dt%</body>@" %%i
grep -Poha "^Last updated .+</body>" "%%i" | sed -r "s@^Last updated (.+)</body>@Last updated %dt%</body>@" %%i
)
--
Edit: Maybe it's a bit extreme in CMD, and I'll just use an AutoIT script instead
Code: Select all
#include <Date.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>
$File = FileOpen(@ScriptDir & "\source.html")
$FileRead = FileRead($File)
FileClose($File)
$replace = "Last updated " & _NowCalcDate() & "</body>"
Local $aArray = StringRegExp($FileRead,"^Last updated (.+)</body>", $STR_REGEXPARRAYMATCH)
if UBound($aArray) then
Local $sOutput = StringRegExpReplace($FileRead, "^(Last updated .+</body>)", $replace)
MsgBox($MB_OK,"Updated",$sOutput)
else
Local $sString = StringReplace($FileRead, "</body>", $replace)
MsgBox($MB_SYSTEMMODAL, "Added", $sString)
EndIf