Parsing a VCF contact file with AutoHotkey

My friend had over 3,000 contacts in his Android phone and we wanted to automate texting & emailing them. Android exports to VCF files but it is a crappy format! Maestrith and I wrote this script which “flattens” the file into a tab delimited format.

Source code for Parsing a VCF contact file with AutoHotkey

FileRead, Text, *P65001 B:\Custom\Win\Desktop\Ryan\example.vcf ; Read data in

Headers:=[], Object:=[] ;Define two objects. one for Headers, one for the data
for a,b in StrSplit(Text,"`n","`r`n"){
		Object.Push(Obj:=[]) ;Create a key placeholder obj for each contact
	}if(SubStr(b,1,4)="End:"||SubStr(b,1,8)="VERSION:") ;Don't grab "End" or "Version"
	Str:=StrSplit(b,":") ;split out b by the :
	if(Str.2&&!InStr(Str.1,"PHOTO;ENCODING")){ ;Only do for those that Values are not "PHOTO;ENCODING"
		Headers[Str.1]:=1 ;create a header with the key
		Obj[Str.1]:= RegExReplace(Str.2,";+",";") ;Replace mulitiple semilcolons with one, then shove data to Object		

;********************Now extract them from the data we've created***********************************
for a,b in Headers
	Total.=a "`t" ;write headers in tab delimmited format
Total:=Trim(Total,"`t") "`r`n" ;remove last tab and add line return and new line

for a,b in Object{ ;Iterate over data object pushign in data
	for c in Headers
		Row.=RegExReplace(b[c],"\t") "`t" ;If there is a tab in the data replace it, then add a tab to the data
	Total.=SubStr(Row,1,-1) "`r`n" ;Drop last tab charachter (Apparently Rtrim with tabs can be problematic) & add new end line/new line
	Row:="" ;Blank out row variable

FileAppend,% Total,Example_Output.txt,UTF-8 ;Write the data to a file

Parsing a VCF contact file with AutoHotkey


Comments are closed.