hello. I want to copy the content of <title>
tag and paste in the <meta keywords..>
tag. Like a PARSING. And I don’t know why, I believe I do something wrong at line 9. Can anyone help me, please?
For example:
<title>MY TITLE</title>
<meta name="keywords" content="BEBE"/>
The output should be:
<title>MY TITLE</title>
<meta name="keywords" content="MY TITLE"/>
$sourcedir = "C:\Folder1"
$resultsdir = "C:\Folder2"
Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {
$content = Get-Content -Path $_.FullName -Raw -Encoding UTF8
$keywords = ([regex]'<meta\s+name="keywords"\s+content="(.+?)"').Match($content)
$title = ([regex]'<title>.*?</title>').Match($content)
if ($keywords.Success -and $title.Success) {
$content = $content.Replace($keywords.Value, '<meta name="keywords" content="' + $title.Value + '"')
$content | Set-Content (Join-Path $resultsdir $_.Name) -Force -Encoding UTF8
}
}