파워셸

파워셸

여러 텍스트 파일의 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>

지원 리소스

관련 정보