Skip to content

Instantly share code, notes, and snippets.

@zflat
Created October 25, 2017 13:18
Show Gist options
  • Save zflat/df1b78bbdbe4089a385c41c761df0410 to your computer and use it in GitHub Desktop.
Save zflat/df1b78bbdbe4089a385c41c761df0410 to your computer and use it in GitHub Desktop.
Simple script to split a CSV into vcard files
#! /usr/bin/ruby
# coding: utf-8
#
# Split a CSV into vcard files
infile = File.new(File.join(File.expand_path(File.dirname(__FILE__)), ARGV), "r")
while (line = infile.gets)
vals = line.split( ",")
continue if vals.empty?
fname = File.join(File.expand_path(File.dirname(__FILE__)), "vcard","#{vals[0]}.vcf".gsub( " ", "").strip)
File.open(fname, "w:UTF-8") do |f|
f.write "BEGIN:VCARD\r\nVERSION:2.1\r\n"
f.write "N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;#{vals[0]};;;\r\n"
f.write "FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:#{vals[0]}\r\n"
f.write "TEL;CELL:#{vals[1].gsub('-', '')}\r\n"
if vals[2].strip! && !vals[2].empty?
f.write "TEL;HOME:#{vals[2].gsub('-', '')}\r\n"
end
f.write "END:VCARD\r\n"
end
end
infile.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment