I've just started using Powershell because I need to make a script that puts links in all files (htm files) of a folder. These links actually link all the files between them.
I have a list of the files that are in the folder (this file is called list.txt
and contains the name of the files without the extension)
In each file I want to make following changes:
From:
<tspan x="53" y="54.8">Surveillance_Err_PRG</tspan>
To:
<tspan x="53" y="54.8"><a href="C:/[...path...]/HTMs/Surveillance_Err_PRG.htm">Surveillance_Err_PRG</a></tspan>
After some research, I wrote following code, but it does nothing (the output just display my code):
$directory = "C:\Users\jacka\Desktop\Organigramme_PLC_prog_test\"
$list = "$directory" + "list.txt"
$htms = "$directory" + "HTMs"
$htmFiles = Get-ChildItem $directory *.htm -rec
foreach ($file in $htmFiles)
{
foreach($line in Get-Content $list)
{
if($line -match $regex)
{
$fichier = "$htms\"+"$line"+".htm"
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "$line", "<a href=""$htms\$line"">$line</a>" } |
Set-Content $file.PSPath
}
echo $fichier
}
}
Before that, I had it like this:
foreach($line in Get-Content $list) {
if($line -match $regex){
$fichier = "$htms\"+"$line"+".htm"
(Get-Content $fichier).replace("$line", "<a href=""$fichier"">$line</a>") | Set-Content $fichier
echo $fichier
}
}
It doesn't really work since it just puts a link on the inner title (in each htm there is the name of the document displayed on the top).
So there is a lot of information I know (but I wanted to give as much information as I could) and I'm sorry if I wasn't clear but basically I want to make the code above work for every file in my folder.
Thanks in advance!