仅查找HTML文件&;在PowerShell中替换其中的标记
我需要在PowerShell中编写一个脚本,该脚本将搜索文件夹,只查找HTML文件,并用新标记替换指定的某行标记 以下是我到目前为止的情况:仅查找HTML文件&;在PowerShell中替换其中的标记,html,powershell,Html,Powershell,我需要在PowerShell中编写一个脚本,该脚本将搜索文件夹,只查找HTML文件,并用新标记替换指定的某行标记 以下是我到目前为止的情况: $filePath = 'C:\Users\bettiom\Desktop\schools\alex\Academics' $processFiles = Get-ChildItem -Exclude *.bak -Filter *.htm -Recurse -Path $filePath $query = '<html>' $replace
$filePath = 'C:\Users\bettiom\Desktop\schools\alex\Academics'
$processFiles = Get-ChildItem -Exclude *.bak -Filter *.htm -Recurse -Path $filePath
$query = '<html>'
$replace = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'
foreach ( $file in $processFiles ) {
$file | Copy-Item -Destination "$file.bak"
$arrayData = Get-Content $file
for ($i=0; $i -lt $arrayData.Count; $i++) {
if ( $arrayData[$i] -match $query ) {
$arrayData[$i+1] = $arrayData[$i+1].Replace($query,$replace)
} else { }
}
$arrayData | Out-File $file -Force
}
$filePath='C:\Users\bettiom\Desktop\schools\alex\Academics'
$processFiles=Get ChildItem-Exclude*.bak-Filter*.htm-Recurse-Path$filePath
$query=''
$replace=''
foreach($processFiles中的文件){
$file |复制项目-目标“$file.bak”
$arrayData=获取内容$file
对于($i=0;$i-lt$arrayData.Count;$i++){
if($arrayData[$i]-match$query){
$arrayData[$i+1]=$arrayData[$i+1]。替换($query,$Replace)
}else{}
}
$arrayData |输出文件$File-强制
}
在foreach循环之前,一切似乎都正常工作,然后它就不会执行该行
任何给予的帮助都将不胜感激
提前感谢。您正在使用不应该使用的管道,并在实际有益的地方避免使用管道。请尝试以下方法:
$filePath = 'C:\Users\bettiom\Desktop\schools\alex\Academics'
$srch = '<html>'
$repl = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'
Get-ChildItem -Exclude *.bak -Filter *.htm -Recurse -Path $filePath | % {
$file = $_.FullName
Copy-Item $file "$file.bak"
(Get-Content $file) -replace $srch, $repl | Out-File $file -Force
}
$filePath='C:\Users\bettiom\Desktop\schools\alex\Academics'
$srch=''
$repl=''
Get-ChildItem-Exclude*.bak-Filter*.htm-Recurse-Path$filePath |%{
$file=$\u.FullName
复制项目$file“$file.bak”
(获取内容$file)-替换$srch,$repl | Out文件$file-强制
}