パワーシェル

パワーシェル

複数のテキスト ファイルから 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ファイル コンテンツの特定の領域内の別のフォルダーにあります。

上記の 2 つの文字列に示されているように、関係する各ファイルについて、これらの値は、コピー元とコピー先を制御するための何らかの区切り文字として使用できると考えられます。

これは両方ですファイル-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>

ファイル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>

サポートリソース

関連情報