Parsing a String into an Object

Hi,

I’m having a mental block with this … I have a report that I’m searching for two values, they alternate throughout the file with an id and a file path. I used regex to select the details I require, as follows:

101

\t60\1000\2000202.doc

102

\t61\1000\200202.doc

I now want to split this into an object with properties of ID and FILEPATH, I defined my properties and created the object ok, but I can’t work out how I should assign each ID and FILEPATH alternating between the properties.

Thanks

Nick

You could do something like this:

$report = New-TemporaryFile

@'
101
\t60\1000\2000202.doc
102
\t61\1000\200202.doc
'@ | Set-Content $report

$match = Select-String -Pattern '^\d{3}$','.+\.doc$' -Path $report.FullName

for ($i = 0; $i -lt $match.Matches.Count; $i = $i + 2) {

    [PSCustomObject]@{
        id   = $match.Matches[$i].Value
        path = $match.Matches[$i + 1].Value
    }

}

Use ReadCount:

$report = Get-Content -Path C:\Scripts\file1.txt -ReadCount 2

foreach ($item in $report) {
    [pscustomobject] @{
        id   = $item[0]
        path = $item[1]
    }
}

Thanks to both of you, I now understand how I can tackle these kinds of challenges! Both great solutions didn’t even know about readcount.