我有很多檔案夾,例如:folder1,folder2,folder3... about folder100
在這些檔案夾中有很多檔案,例如:1.html,2.html,3.html,4.html...about 20.html
我想替換所有檔案夾中所有 html 檔案中的一些文本,但并非所有要替換的文本都相同。例如:(對于 1.html,我想將 ./1_files/style.css 替換為 style.css)和(對于 2.html,我想將 ./2_files/style.css 替換為 style.css)... .
所以我嘗試了這樣的事情并且效果很好
Get-ChildItem "*\1.html" -Recurse | ForEach-Object -Process {
(Get-Content $_) -Replace './1_files/style.css', 'style.css' | Set-Content $_
}
Get-ChildItem "*\2.html" -Recurse | ForEach-Object -Process {
(Get-Content $_) -Replace './2_files/style.css', 'style.css' | Set-Content $_
}
Get-ChildItem "*\3.html" -Recurse | ForEach-Object -Process {
(Get-Content $_) -Replace './3_files/style.css', 'style.css' | Set-Content $_
}
Get-ChildItem "*\4.html" -Recurse | ForEach-Object -Process {
(Get-Content $_) -Replace './4_files/style.css', 'style.css' | Set-Content $_
}
但我必須寫很多這樣的代碼“ \4.html”“ \5.html”“*\6.html”......
我試試這個,但它不起作用
Do {
$val
Write-Host $val
$Fn = "$val.html"
Get-ChildItem "*\$Fn" -Recurse | ForEach-Object -Process {
(Get-Content $_) -Replace './$val_files/style.css', 'style.css' |
Set-Content $_
}
} while($val -ne 100)
請告訴我正確的做法..回圈替換 謝謝
uj5u.com熱心網友回復:
假設您的所有子檔案夾都可以在一個源檔案夾路徑中找到,您可以執行以下操作來替換所有這些檔案:
# the path where all subfolders and html files can be found
$sourcePath = 'X:\Wherever\Your\Subfolders\Are\That\Contain\The\Html\Files'
Get-ChildItem -Path $sourcePath -Filter '*.html' -Recurse -File |
# filter on html files that have a numeric basename
Where-Object {$_.BaseName -match '(\d )'} | ForEach-Object {
# construct the string to repace and escape the regex special characters
$replace = [regex]::Escape(('./{0}_files/style.css' -f $matches[1]))
# get the content as one single multiline string so -replace works faster
(Get-Content -Path $_.FullName -Raw) -replace $replace, 'style.css' |
Set-Content -Path $_.FullName
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393500.html
標籤:电源外壳
