Power Shell

Power Shell

Ich versuche, HTML-Inhalte aus mehreren Textdateien zu analysieren, bestimmte Teile jeder Datei auszuwählen und diesen Inhalt zu kopieren, um ihn später in den Inhalt einer anderen Datei einzufügen.

Der ausgewählte Wert wird in einen bestimmten Teil des Inhalts einer anderen Datei eingefügt, der einen übereinstimmenden Dateinamen hat, sich in einem anderen Verzeichnis befindet und andere Inhalte enthält.

Die Datei, die mit dem ausgewählten Teil des Inhalts der anderen Datei aktualisiert wird, weist Inhalte auf, die beibehalten werden müssen und unverändert bleiben müssen.


Mehr Details

Angenommen, ich habe diesen HTML-Code vonDATEI-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>

Ich möchte den gesamten Textinhalt kopieren vonDATEI-1.htmlbeginnt um...

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

    den ganzen Weg zu

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

und injizieren Sie dies inDatei-2.htmldas sich in einem anderen Ordner in einem bestimmten Bereich seines Dateiinhalts befindet.

Ich vermute, dass diese Werte, wie in den beiden obigen Zeichenfolgen gezeigt, für jede beteiligte Datei als eine Art Trennzeichen verwendet werden können, um zu steuern, woher und wohin kopiert werden soll.

Dies ist beidesDATEI-1.htmlUndDatei-2.htmlnebeneinander mit Anmerkung:

Bildbeschreibung hier eingeben

Datei-2.htmldas aktualisiert werden muss, könnte folgendermaßen aussehen:

<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>

Im Grunde kopiere ich nur den Textartikel aus einer Datei, um ihn in eine andere Datei einzufügen. Dieser Text, der auch in der Klasse enthalten ist, wird zwischen die Kommentare eingefügt.

Das Problem besteht darin, dass ich 3.000 Dateien geändert habe und dies manuell nur für viel kleinere Dateisätze getan habe. Daher versuche ich, eine effizientere Methode zu finden.

Beispiel für Dateiverzeichnisse Bildbeschreibung hier eingeben

Sie können lange Kommentare wie <!-- * * * * * START HERE * * * * * -->und <!-- * * * * * END HERE * * * * * -->durch etwas Kurzes wie <!--START1-->und ersetzen.<!--FINNISH1-->

DIE BESTE LÖSUNGIST DIES EINE, mitPOWER SHELL

$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

Antwort1

Mit Windows PowerShell geht das wahrscheinlich besser, aber da Sie PowerShell gerne verwenden, hier eine Variante zum Einstieg, mit der es nach meinen Tests funktionieren sollte.

Power Shell

$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; 
        
        }
}};

Beispiele für Vorher- und Nachher-Ergebnisse

File-1.html (wird für Aktualisierungsinhalte verwendet)

<!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 (vor dem Update)

<!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 (nach Update)

<!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>

Unterstützende Ressourcen

verwandte Informationen