2013-12-10 41 views
0

我想解析robocopy日志文件来获取文件大小,路径和日期修改。我通过正则表达式获取信息,没有问题。但是,出于某种原因,我得到一个单个元素的数组,并且该元素包含3个哈希值。我的术语可能会关闭;我仍然在学习哈希。我想要的是一个具有多元素的常规数组。一个包含哈希而不是多个元素的元素 - 如何解决?

输出我得到:

FileSize       FilePath       DateTime       
--------       --------       --------       
{23040, 36864, 27136, 24064...} {\\server1\folder\Test File R... {2006/03/15 21:08:01, 2010/12... 

正如你所看到的,只有一排,但该行包含多个项目。我想要多行。

这里是我的代码:

[regex]$Match_Regex = "^.{13}\s\d{4}/\d{2}/\d{2}\s\d{2}:\d{2}:\d{2}\s.*$" 
[regex]$Replace_Regex = "^\s*([\d\.]*\s{0,1}\w{0,1})\s(\d{4}\/\d{2}\/\d{2}\s\d{2}:\d{2}:\d{2})\s(.*)$" 

$MainContent = New-Object System.Collections.Generic.List[PSCustomObject] 

Get-Content $Path\$InFile -ReadCount $Batch | ForEach-Object { 
    $FileSize = $_ -match $Match_Regex -replace $Replace_Regex,('$1').Trim() 
    $DateTime = $_ -match $Match_Regex -replace $Replace_Regex,('$2').Trim() 
    $FilePath = $_ -match $Match_Regex -replace $Replace_Regex,('$3').Trim() 

    $Props = @{ 
     FileSize = $FileSize; 
     DateTime = $DateTime; 
     FilePath = $FilePath 
    } 
    $Obj = [PSCustomObject]$Props 
    $MainContent.Add($Obj) 
} 

$MainContent | % { 
    $_ 
} 

我在做什么错?我只是没有得到它。谢谢。

注意:这需要尽可能快,因为我必须处理数百万行,这就是为什么我在尝试System.Collections.Generic.List。

回答

1

我认为问题在于,对于你在做什么而言,实际上需要两个foreach对象循环。使用Get-Content和-Readcount会给你一个数组的数组。使用第一个Foreach-Object中的-Match过滤出每个数组中匹配的记录。这将会给你一组匹配的记录。然后,你需要通过数组的foreach创建一个对象的每个记录:

[regex]$Match_Regex = "^.{13}\s\d{4}/\d{2}/\d{2}\s\d{2}:\d{2}:\d{2}\s.*$" 
[regex]$Replace_Regex = "^\s*([\d\.]*\s{0,1}\w{0,1})\s(\d{4}\/\d{2}\/\d{2}\s\d{2}:\d{2}:\d{2})\s(.*)$" 

$MainContent = 
    Get-Content $Path\$InFile -ReadCount $Batch | 
    ForEach-Object { 
    $_ -match $Match_Regex | 
    ForEach-Object { 
     $FileSize = $_ -replace $Replace_Regex,('$1').Trim() 
     $DateTime = $_ -replace $Replace_Regex,('$2').Trim() 
     $FilePath = $_ -replace $Replace_Regex,('$3').Trim() 

     [PSCustomObject]@{ 
     FileSize = $FileSize 
     DateTime = $DateTime 
     FilePath = $FilePath 
     } 
    }  
} 

你并不真的需要使用集合作为累加器,只输出PSCustomObjects,并让他们在结果变量累积。

+0

工作正常!非常感谢! –

相关问题