用powershell替换文件夹中所有文件中的字符串

夹克

我刚刚开始使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Powershell替换文件夹中所有文件中的字符串

替换文件夹中所有文件中的字符串

如何替换文件夹中Word文档中所有出现的字符串

ExcelVBA-替换指定文件夹中所有文件名中的字符串

如何获得否。与文件夹中所有文件中的字符串匹配的行数

Bash脚本替换文件中所有出现的字符串,包括文件名

Sed - 替换文件倒数第二行中所有出现的字符串

Powershell更新所有子文件夹中每个文件中的字符串

用括号中的字符替换文件夹名称

Bash文件夹中所有文件名中都出现字符串

替换文件夹中所有.sh文件的第一行

计算一个文件夹中所有文件中所有出现的字符串

在文件夹及其所有子文件夹的所有文件中搜索并替换另一个子字符串

替换文件中的所有字符串,在替换中使用通配符

用nodejs替换文件中的字符串

用SED替换文件中的字符串

用多行字符串替换文件中的文本

用perl替换文件中的字符串

用脚本替换文件中的字符串

Bash:用数组替换文件中的字符串

如何在所有文件夹和文件名中替换字符串

Powershell-用通配符替换文件中的字符串

用另一个图像替换文件夹中的所有图像,但保留文件名

如何使用Groovy读取文件夹中的所有文件并替换文件中的模式

替换文件中的字符串

替换文件中的字符串

如何替换文件中以某个前缀开头的所有字符串

替换文件中的字符串将覆盖所有内容

使用Ant,替换文件中的所有_,但仅替换特定字符串中的所有_吗?