我試圖從多個文字檔案中解析 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開始於...
<!-- * * * * * START HERE * * * * * -->
一直到
<!-- * * * * * 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 * * * * * -->
and 並<!-- * * * * * END HERE * * * * * -->
替換為短註釋,例如<!--START1-->
and<!--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>
支持資源
-
標準別名對於 Foreach-Object:「
%
」符號,ForEach -
A。`t: 水平製表符
b.`n: 新隊
C。`r: 回車
其中c.和b。 :
CRLF
停產