电源外壳

电源外壳

我正在尝试解析来自多个文本文件的 HTML 内容并选择每个文件的特定部分并复制该内容以便稍后粘贴到另一个文件的内容中。

所选值将被注入到具有匹配文件名、位于不同目录中且包含不同内容的另一个文件内容的特定部分中。

使用来自另一个文件的选定部分内容进行更新的文件将包含一些需要保留并保持不变的内容。


更多详情

假设我有以下 html 代码文件-1.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="ro">
<title>YES, I love her</title>
<head>
</head>
<body>
...
<div id="coloana_centru">
  <div class="container mM" id="incadrare_text_mijloc_2" itemscope itemtype="http://schema.org/Product">
    <div align="justify">

        <!-- * * * * * START HERE * * * * * -->

        <p class="TATA"><em>At the mobile site I put as the header location in case the device isn't mobile? And then it executes the php code you gave stack..</em></p>
        <p class="MAMA">Simply check if the referrer is coming from within your site. If they are, they have already seen a page and have chosen where they want to go next</p>

        <!-- * * * * * END HERE * * * * * -->
</div>
</div>
</body>
</html>

我想复制整个文本内容文件-1.html开始于...

  1. <!-- * * * * * START HERE * * * * * -->

    一直到

  2. <!-- * * * * * END HERE * * * * * -->

并将其注入文件-2.html它位于文件内容的特定区域的不同文件夹中。

对于我怀疑的每个相关文件,如上面两个字符串所示,这些值可以用作某种分隔符来控制从哪里复制以及复制到哪里。

这两者都是文件-1.html文件-2.html并列注释:

在此处输入图片描述

文件-2.html需要更新的内容可能如下所示:

<section class="page_header pb-0 w-100">
    <div class="bg-overlay bg-black opacity-7"></div>
    <div class="container">
        <div class="row">
            <div class="col-md-12 page-content position-relative text-white text-center">
                <h2 class="mb-3">Blog Content</h2>
                <h6 class="mb-2 text-white text-capitalize">Creativity leads to new inventions </h6>
                <a href="../index-creative-studio.html" class="d-inline-block text-white">Home</a> <span><i class="fa fa-angle-double-right font-13"></i> Blog</span>
            </div>
        </div>
    </div>
</section>
<div id="bingo">
  <div class="container good-job" id="love" itemscope itemtype="http://schema.org/Product">
   <div class="blog-listing-inner news_item">

        <!-- * * * * * START HERE * * * * * -->

        <p class="TATA"><em> Other text 1 </em></p>
        <p class="MAMA"> Other text 2</p>

        <!-- * * * * * END HERE * * * * * -->

<div class="profile-authors heading-space-half">
    <h4 class="text-capitalize color-black mb-35px">Comments</h4>
        <div class="any-profile mb-30px">
            <div class="profile-photo"><img src="img/post6.jpg" alt="Comments"> </div>

基本上,我只是将文本文章从一个文件复制到另一个文件中。该文本也与课程一起放在评论之间。

问题是我已经更改了 3,000 个文件,并且我只对较小的文件集手动执行此操作,因此尝试找出一种更有效的方法。

文件目录示例 在此处输入图片描述

您可以将长注释替换为和<!-- * * * * * START HERE * * * * * -->,并将其替换<!-- * * * * * END HERE * * * * * -->为简短的注释,例如<!--START1--><!--FINNISH1-->

最佳解决方案这是使用电源外壳

$sourceFiles = Get-ChildItem 'c:\Folder1'  
$destinationFolder = 'c:\Folder2'

foreach ($file in $sourceFiles) {

$sourceContent = Get-Content $file.FullName -Raw
$contentToInsert = [regex]::match($sourceContent,"(?ms)<!--START1-->(.+)<!--FINNISH1-->").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '(?ms)<!--START1-->(.+)<!--FINNISH1-->',$contentToInsert

Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8

} #end foreach file

答案1

使用 Windows PowerShell 可能有更好的方法来执行此操作,但既然您愿意使用 PowerShell,那么这里有一个变体可以帮助您,根据我的测试,它应该可以解决问题。

电源外壳

$src = Get-ChildItem -Path "c:\Folder-1" -Filter "*.html";
$destFld = "c:\Folder-2";

$src | % { Process {
 
    If ( Test-Path "$destFld\$($_.Name)" ) { 

        Clear-Variable -Name ("a","b","c","x","y","z");
        $z = Get-Content $_.FullName -Raw;
        $y = "`t`t<!-- $((($z -split "<!--")[1]).Trim())`r`n";
        $x = "`t`t<!-- * * * * * END HERE * * * * * -->";
        $a = Get-Content "$destFld\$($_.Name)" -Raw;
        $b = "$(($a -split "<!--")[0].Trim())`r`n";
        $c = (($a -split "<!--")[2] -Split "-->")[1].Trim();
        $b | Out-File "$destFld\$($_.Name)"; 
        $y | Out-File "$destFld\$($_.Name)" -Append; 
        $x | Out-File "$destFld\$($_.Name)" -Append; 
        $c | Out-File "$destFld\$($_.Name)" -Append; 
        
        }
}};

前后结果示例

File-1.html(用于更新内容)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="ro">
<title>YES, I love her</title>
<head>
</head>
<body>
...
<div id="coloana_centru">
  <div class="container mM" id="incadrare_text_mijloc_2" itemscope itemtype="http://schema.org/Product">
    <div align="justify">

        <!-- * * * * * START HERE * * * * * -->

        <p class="TATA"><em>At the mobile site I put as the header location in case the device isn't mobile? And then it executes the php code you gave stack..</em></p>
        <p class="MAMA">Simply check if the referrer is coming from within your site. If they are, they have already seen a page and have chosen where they want to go next</p>

        <!-- * * * * * END HERE * * * * * -->
</div>
</div>
</body>
</html>

File-2.html(更新前)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="ro">
<title>No, I do NOT love her because she too ugly</title>
<head>
</head>
<body>
...
<div id="SheTooUgly">
  <div class="container mM" id="incadrare_doc_mijloc_33" itemscope itemtype="http://schema.org/Product">
    <div align="justify">

        <!-- * * * * * START HERE * * * * * -->

        <p class="TATA"><em>Girl too ugly to love</em></p>
        <p class="MAMA">She way too ugly, yuk</p>

        <!-- * * * * * END HERE * * * * * -->
</div>
</div>
</body>
</html>

File-2.html(更新后)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="ro">
<title>No, I do NOT love her because she too ugly</title>
<head>
</head>
<body>
...
<div id="SheTooUgly">
  <div class="container mM" id="incadrare_doc_mijloc_33" itemscope itemtype="http://schema.org/Product">
    <div align="justify">

        <!-- * * * * * START HERE * * * * * -->

        <p class="TATA"><em>At the mobile site I put as the header location in case the device isn't mobile? And then it executes the php code you gave stack..</em></p>
        <p class="MAMA">Simply check if the referrer is coming from within your site. If they are, they have already seen a page and have chosen where they want to go next</p>

        <!-- * * * * * END HERE * * * * * -->
</div>
</div>
</body>
</html>

支持资源

相关内容