Parsing a file to create an array of lines

前端 未结 2 550
再見小時候
再見小時候 2020-12-06 22:26

This seems so incredibly simple but I am missing something. I just need to add an array to array[0], array[1], etc. I am taking a vcard file and trying to read all the lines

2条回答
  •  执念已碎
    2020-12-06 22:59

    This should do exactly what you want:

    Add-Type -AssemblyName System.Collections
    
    [System.Collections.Generic.List[object]]$allContacts = @()
    [System.Collections.Generic.List[string]]$contact = @()
    
    $filePath  = 'C:\temp\Contacts_Backup.vcf'
    $endMarker = 'END:VCARD'
    
    foreach($line in [System.IO.File]::ReadLines($filePath))
    {
            if( $line -eq $endMarker ) {
                $allContacts.Add( $contact.ToArray() )
                $contact.Clear()
            }
            else {
                $contact.Add( $line )
            }
    }
    
    # Ready. Show result.
    
    foreach( $vcf in $allContacts ) {
    
        "Contact: "
        $vcf
    
    }
    

提交回复
热议问题