2016-12-17 43 views
0

我需要遍歷在一個目錄中找到的.vcf文件,以使用powershell將它們合併成一個.vcf文件。我希望N:的值是文件名。這是我的,它不工作。請幫忙。使用PowerShell循環使用VCF文件生成一個VCF

$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") 
} 

謝謝。

更新:我得到了它使用此代碼工作(代碼對於調試變得簡單)

$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 
} 

這就產生了一個文件,所有VCF信息相結合。 我遇到的困難是因爲我不知道.vcf文件如何工作。很簡單。

+0

這是什麼代碼產生的工作?任何錯誤消息?錯誤的文件名?請將結果複製並粘貼到問題中。 – lit

+0

如果您自己解決問題,請發表解答 –

回答

0

我得到了它使用此代碼

$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 
}