我刚刚开始使用Powershell,因为我需要制作一个脚本,将链接放入文件夹的所有文件(htm文件)中。这些链接实际上链接了它们之间的所有文件。我有一个文件夹中文件的列表(此文件被称为list.txt
并且包含不带扩展名的文件的名称)
我想在每个文件中进行以下更改:
从:
<tspan x="53" y="54.8">Surveillance_Err_PRG</tspan>
至:
<tspan x="53" y="54.8"><a href="C:/[...path...]/HTMs/Surveillance_Err_PRG.htm">Surveillance_Err_PRG</a></tspan>
经过一番研究,我编写了以下代码,但是什么也不做(输出只显示我的代码):
$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
}
}
在那之前,我是这样的:
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
}
}
它实际上并没有用,因为它只是在内部标题上放置了一个链接(在每个htm中,顶部都显示了文档的名称)。
因此,我知道很多信息(但是我想提供尽可能多的信息),很抱歉,如果我不清楚,但基本上我想使上面的代码适用于文件夹中的每个文件。
提前致谢!
所以我找到了解决方案
首先,我在那里遇到问题
$htmFiles = Get-ChildItem $directory *.htm -rec
foreach ($file in $configFiles)
变量不一样,但是然后我得到了这个错误:
C:\Users\jacka\Desktop\Organigramme_PLC_prog_test\HTMs\Systeme_Filtration_Prg.htm
Get-Content : Impossible de trouver le chemin d'accès « C:\Users\jacka\ChargementProg_PRG.htm », car il n'existe pas.
Au caractère Ligne:22 : 14
+ (Get-Content $file) |
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\jacka\ChargementProg_PRG.htm:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
我通过在$ file之后添加.FullName来解决此问题,这阻止了Get-Content尝试从当前目录访问文件:
$htmFiles = Get-ChildItem $directory *.htm -rec
foreach ($file in $htmFiles)
{
foreach($line in Get-Content $list)
{
if($line -match $regex)
{
$fichier = "$directory"+"$line"+".htm"
if ($file.FullName -ne $fichier) #to prevent header to be changed
{
(Get-Content $file.FullName) |
Foreach-Object { $_ -replace "$line", "<a href=""$fichier"">$line</a>" } |
Set-Content $file.FullName
}
}
}
echo "$file.FullName is done"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句