Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在PowerShell中使用此命令?_Windows_Powershell_Replace - Fatal编程技术网

Windows 如何在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返回一个行数组,而不是作为单个字符串返回文件的全文 如果您所做的只是尝试从每个

我需要对一个大约3000个文档的库运行以下命令,但由于无法获得一个有效的正则表达式(不是我最擅长的),或者与PowerShell中的
/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" } | % {