Windows 如何在PowerShell中使用此命令?
我需要对一个大约3000个文档的库运行以下命令,但由于无法获得一个有效的正则表达式(不是我最擅长的),或者与PowerShell中的Windows 如何在PowerShell中使用此命令?,windows,powershell,replace,Windows,Powershell,Replace,我需要对一个大约3000个文档的库运行以下命令,但由于无法获得一个有效的正则表达式(不是我最擅长的),或者与PowerShell中的/v选项等效的正则表达式,我不得不执行以下命令。有人能给我指一下正确的方向吗 我的命令 C:\findstr /v "<?xml version=" filename.htm > ..\testOut\filename.htm C:\findstr/v”Get Content返回一个行数组,而不是作为单个字符串返回文件的全文 如果您所做的只是尝试从每个
/v
选项等效的正则表达式,我不得不执行以下命令。有人能给我指一下正确的方向吗
我的命令
C:\findstr /v "<?xml version=" filename.htm > ..\testOut\filename.htm
C:\findstr/v”Get Content
返回一个行数组,而不是作为单个字符串返回文件的全文
如果您所做的只是尝试从每个文件中删除xml声明,请尝试此操作,假设$srcfiles
是完整文件路径的集合:
foreach($file in $srcfiles)
{
$content = Get-Content $file | ? { $_ -notmatch "<\?xml[^>]+>" }
$content | Set-Content $file -Force
}
foreach($srcfiles中的文件)
{
$content=Get content$file |?{$\u0-notmatch“]+>”}
$content |设置内容$file-强制
}
基本上,循环遍历所有文件,获取每个文件的内容,忽略任何xml声明行,然后将数据推回到原始文件。我分两步执行此操作,因为PowerShell不允许您在获取数据的同一管道中将内容写入文件
$path = "C:\Path\To\Documents"
$outputPath = "C:\Path\To\OutputDocuments"
Get-ChildItem $path | % {
$content = ( Get-Content -Raw $_ ) -replace '<?xml version="1.0" encoding="utf-8"?>', ''
$fileName = Join-Path $outputPath $_.Name
Set-Content -Path $fileName -Value $content
}
$path = "C:\Path\To\Documents"
$outputPath = "C:\Path\To\OutputDocuments"
Get-ChildItem $path | % {
$content = ( Get-Content -Raw $_ ) -replace '<?xml version="1.0" encoding="utf-8"?>', ''
$fileName = Join-Path $outputPath $_.Name
Set-Content -Path $fileName -Value $content
}
Get-ChildItem $path | ? { $_.GetType() -eq "FileInfo" } | % {