Loop through VCF files using powershell to generate one VCF
<p>I need to loop through .vcf files found in one directory to merge them into one single .vcf file using powershell. I want the <code>N:</code> value to be the file name. This is what I have and it's not working. please help.</p>

<pre><code>$fileDirectory = "c:\Contacts";
$parse_results = New-Object System.Collections.ArrayList;
$vcard = ""
foreach($file in Get-ChildItem $fileDirectory)
{
$contacts = ConvertFrom-Csv (Get-Content $file.FullName).Replace(';',',')
foreach ($contact in $contacts) {
        $vcard = $vcard + "BEGIN:VCARD" + "`r`n"
        $vcard = $vcard + "VERSION:3.0" + "`r`n"
        $vcard = $vcard + "N:" + $file.Basename + ";" + "" + "`r`n"
        $vcard = $vcard + "FN:" + "" + " " + "" + "`r`n"
        $vcard = $vcard + "TEL:" + $contact."general phone" + "`r`n"
        $vcard = $vcard + "EMAIL:" + $contact."general email" + "`r`n"
        $vcard = $vcard + "END:VCARD" + "`r`n"
    }
    $vcard | Add-Content ($File.Basename + ".vcf")
}
</code></pre>

<p>Thank you.</p>

<p>UPDATE: I got it to work using this code (Code is made simple for debugging purposes)</p>

<pre><code>$files = Get-ChildItem "C:\Contacts\"
for ($i=0; $i -lt $files.Count; $i++) {
 $string = ""
 $string = Get-Content $files[$i].FullName | Out-String
 $newName = "N:" + $files[$i].BaseName
 $string -replace '(?m)^N:{1}\W.*$', $newName
}
</code></pre>

<p>This yields one file with all vcf info combined.
The difficulties I had were because I didn't know well how .vcf files work. So simple.</p>