我需要遍历在一个目录中找到的.vcf文件,以便使用powershell将它们合并为一个单独的.vcf文件。我希望N:值是文件名。这就是我所拥有的,但它不起作用。请帮帮忙。
$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文件是如何工作的。太简单了。
发布于 2016-12-18 00:47:03
我使用下面的代码让它正常工作
$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
}https://stackoverflow.com/questions/41194570
复制相似问题