【发布时间】:2022-01-25 10:49:06
【问题描述】:
我有很多文件夹 例如:文件夹1,文件夹2,文件夹3...关于文件夹100
在那些文件夹中有很多文件 例如:1.html,2.html,3.html,4.html...大约 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)
请告诉我正确的做法..循环替换 谢谢您
【问题讨论】:
-
请添加您要更改的数据文件之一的示例以及结果的外观。请将其添加到您的问题中并以代码格式包装,以便人们可以轻松找到并阅读它。
-
-Replace './$val_files/style.css'-->-Replace "./$val_files/style.css"。不插入单引号字符串。 -
顺便说一句,
-Recurse是多余的,因为它仅在-Path参数指向文件夹时才有效。您甚至可以将Get-ChildItem替换为Get-Item。
标签: powershell