我遇到问题,循环遍历目录中的所有文件并根据字段“[IMPORT]”将其拆分
目录:\ C \ users $ \ Pepe \ Desktop \ TestInfoFolder
目录中有以下两个文件: TestingInfo1.txt TestingInfo2.txt
他们看起来像这样:
TestingInfo1.txt
[IMPORT]
1
2
3
4
5
6
7
8
9
10
[IMPORT]
11
12
13
14
15
16
17
18
19
20
TestingInfo2.txt
[IMPORT]
21
22
23
24
25
所需的结果将是3个单独的文件拆分字符串“[IMPORT]”,这包含在文件中。
结果1:
[IMPORT]
1
2
3
4
5
6
7
8
9
10
结果2:
[IMPORT]
11
12
13
14
15
16
17
18
19
20
结果3:
[IMPORT]
21
22
23
24
25
当前代码:
$Path = "\\C\users$\Pepe\Desktop\TestInfoFolder"
Get-ChildItem $Path | foreach-object {
$InputFile = $_.FullName
$Reader = New-Object System.IO.StreamReader($InputFile)
$N = 1
While (($Line = $Reader.ReadLine()) -ne $null) {
if ($Line -match "[IMPORT]") {
$OutputFile = $matches[0] + $N + ".txt"
$N++
}
Add-Content (Join-Path $Path $OutputFile) $Line
}
}
当前查询的问题是它将ResultsInfo2.txt的结果附加到结果1的结果中。如果您需要进一步说明,请与我们联系。
答案 0 :(得分:1)
正则表达式是你的朋友。这应该为$ Path中的所有文件提供IMPORT序列的所有实例:
ls $Path | cat | select-string -AllMatches "(\[IMPORT\][\s*\d+]+)" | % {$_.Matches.Value}
答案 1 :(得分:1)
我会做类似于Jon Dechiro的事情,但我会将所有文本作为多行字符串收集到一个变量中,然后将其拆分为包含从[IMPORT]
到下一个FOR
的所有内容的块(或者文件的结尾),然后输出$Path = "C:\Users\Pepe\Desktop\TestInfoFolder"
$InputText = (Get-Content "$Path\*.txt" -Raw) -join "`r`n"
$SplitText = $InputText -split "(?s)(\[IMPORT].*?)(?=\[IMPORT]|$)"|?{$_}
New-Item -Path $Path\Output -ItemType Directory -Force|Out-Null
For($i=0;$i -le $SplitText.Count;$i++){
$SplitText[$i] | Set-Content "$Path\Output\Results$($i+1).txt" -Force
}
循环中的每个项目:
<div class="ui-grid-viewport ng-isolate-scope" ng-style="colContainer.getViewportStyle()" role="rowgroup" ui-grid-viewport="" style="overflow-x: hidden; overflow-y: scroll;">
<div class="ui-grid-canvas">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index" style="margin-top: 630px;">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index" style="">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
<div class="ui-grid-row ng-scope" ng-style="Viewport.rowStyle(rowRenderIndex)" ng-repeat="(rowRenderIndex, row) in rowContainer.renderedRows track by $index">
</div>
</div>
这将创建一个文件夹&#34;输出&#34;在路径中,并将结果保存在那里。
答案 2 :(得分:0)
我会这样做:
$SourcePath = "C:\Users\jon_dechiro\Desktop\Test"
$OutputPath = "C:\Temp"
$Count = 1
foreach ($File in (Get-ChildItem -Path $SourcePath -File))
{
$FileRawContents = Get-Content $File.FullName -Raw
foreach ($Import in ($FileRawContents -split '\[IMPORT\]'))
{
if ($Import -match '\d')
{
$ImportEdited = $Import -replace '\n', "`r`n"
"[IMPORT]$ImportEdited" | Out-File "$OutputPath\Results$Count.txt"
$Count++
}
}
}
打开每个文件并按[IMPORT]拆分,然后按照指示将每个结果发送到输出文件。当然,将$ SourcePath和$ OutputPath变量更改为您需要的。