Séparer fusionner fichier VCF (Split Separate Merge VCF File)

Fermé
Celestus Messages postés 11 Date d'inscription dimanche 30 septembre 2007 Statut Membre Dernière intervention 24 novembre 2016 - Modifié par Celestus le 8/09/2016 à 00:32
Bonjour,
Je n'ai pas de question, je mets un code au service de la communauté.

Quand on veut exporter ses contacts sous Android, il les exporte notre liste de contacts sous la forme d'un fichier .VCF. Le plus souvent appelé "00001.vcf".

----------------------
Hello,
I have no questions, I release a code for the community.

When you want to export your contacts on Android, it exports your contact list as a .VCF file. Usually called "00001.vcf".
----------------------


______________________________________________________
Le format "VCF" ou "VCard" ou "Virtual Card" est le format texte habituel pour encoder ses contacts.
Un fichier au format ".vcf" est toujours encodé en ANSI.

Il ne faut pas mettre plusieurs contacts dans un même fichier.
On met un contact par fichier ".vcf".

En effet, au-delà d'une certaine taille (environ 3 Mo), il arrive qu'il y ait des bugs au moment d'importer le fichier sous Android et que des contacts soient manquants.
Ces problèmes n'apparaissent pas lorsque chaque contact possède son propre fichier ".vcf".

Chaque fichier ".vcf" a une limite de taille (environ 3 Mo).
Donc si vous ajoutez des photos pour certains contacts et que tous vos contacts sont dans un fichier ".vcf", vous ne pouvez pas mettre de grosses photos, sinon des bugs apparaîtront et certains contacts ne seront pas ajoutés à votre répertoire.

Mieux vaut éviter de mettre des photos de moins de 150 x 150 Pixels car la définition devient vraiment mauvaise.
______________________________________________________

Sur Internet, je ne trouve jamais de script pour "séparer" tous les contacts présents dans un fichier ".vcf", ni pour fusionner plusieurs fichiers ".vcf" ensemble.
J'ai donc décidé d'écrire ces scripts.

Le principe est simple :
- Démasquez vos extensions
- Créez un nouveau document texte
- Ouvrez-le dans le bloc-notes
- Fichier > Enregistrer sous
- Enregistrez-le au format Unicode (c'est-à-dire UTF-16 LE pour les puristes)
- Copiez dedans le code ci-dessous
- Renommez l'extension ".txt" du fichier en ".vbs"

----------------------
On the Internet, I never find script to "split" all contacts in a ".vcf" file or to merge multiple ".vcf" files together.
So I decided to write these scripts.

The principle is simple:
- Unmask your extensions
- Create a new text document
- Open it in Notepad
- File > Save As
- Save it in Unicode format (means UTF-16 LE for purists)
- Copy in the code below
- Rename the ".txt" file extension to ".vbs"
----------------------


______________________________________________________

Voici le premier script : il sépare.
Il lit le fichier appelée "Contacts.vcf" et il crée un fichier .vcf par contact.

----------------------
This is the first script: it splits.
It read the file called "Contacts.vcf" and it creates a .vcf file by contact.
----------------------

file_name       = "Contacts.vcf"
folder_name     = "Contacts - Séparation"
'                 "Contacts - Split"
standard_name   = "Contact_"
error_message_1 = "Erreur :" & vbCrLf & "Le fichier """ & file_name & """ n'existe pas."  & vbCrLf & "Il doit être placé dans le même dossier que ce script."
'                 "Error:"   & vbCrLf & "The file """   & file_name & """ doesn't exist." & vbCrLf & "It has to be located in the same folder as this script."
error_message_2 = "Erreur :" & vbCrLf & "Le dossier de destination """ & folder_name & """ existe déjà."    & vbCrLf & "Veuillez renommer ou supprimer le dossier existant."
'                 "Error:"   & vbCrLf & "The destination folder """    & folder_name & """ already exists." & vbCrLf & "Please rename or remove the existing folder."
end_message     = "Nombre de fichiers extraits : "
'                 "Number of files extracted: "
string_start    = "BEGIN:VCARD"
string_end      = "END:VCARD"
string_name     = "FN:"

'__________________________________________________________________________________________________

dim file_system_object
set file_system_object = CreateObject("Scripting.FileSystemObject")

if (file_system_object.FileExists(LCase(file_name)) = FALSE) then
	WScript.Echo(error_message_1)
	WScript.Sleep 5000
	WScript.Quit(-1)
end if

if (file_system_object.FolderExists(LCase(folder_name)) = FALSE) then
	file_system_object.CreateFolder(folder_name)
else
	WScript.Echo(error_message_2)
	WScript.Sleep 5000
	WScript.Quit(-2)
end if

dim input_file
set input_file = file_system_object.OpenTextFile(file_name, 1, FALSE, TristateUseDefault)	' 1 = ForReading
dim output_file

b_switch               = FALSE
i_step                 = 0
i_count                = 1
i_count_2              = 1
i_count_file           = 0
c_character            = ""
file_name              = ""
standard_name_formated = ""
number_of_digits       = 6

Do Until input_file.AtEndOfStream
	c_character = input_file.Read(1)

	if (i_step = 0) then
		if (c_character = Mid(string_start,i_count,1)) then
			i_count = i_count + 1
		else
			i_count = 1
			i_step  = 0
			if (c_character = Mid(string_start,i_count,1)) then
				i_count = i_count + 1
			end if
		end if
		if (i_count >= Len(string_start) + 1) then
			i_count = 1
			i_step  = 1
		end if
	end if
	if (i_step = 1) then
		i_count_file = i_count_file + 1
		if (number_of_digits - Len(CStr(i_count_file))) < 0 then
			number_of_digits = Len(CStr(i_count_file))
		end if
		standard_name_formated = standard_name & String(number_of_digits - Len(CStr(i_count_file)),"0") & CStr(i_count_file)
		set output_file = file_system_object.OpenTextFile(folder_name & "/" & standard_name_formated & ".vcf", 2, TRUE, TristateFalse)	' 2 = ForWriting     TristateFalse = ASCII
		output_file.Write(string_start)
		i_step   = 2
		b_switch = TRUE
	end if

	if (i_step >= 2) then
		if (b_switch = FALSE) then
			output_file.Write(c_character)
		end if
		b_switch = FALSE
	end  if

	if (i_step = 2) then
		if (c_character = Mid(string_name,i_count_2,1)) then
			i_count_2 = i_count_2 + 1
		else
			i_count_2 = 1
			i_step    = 2
			if (c_character = Mid(string_name,i_count_2,1)) then
				i_count_2 = i_count_2 + 1
			end if
		end if
		if (i_count_2 >= Len(string_name) + 1) then
			i_count_2   = 1
			i_step      = 3
			b_switch    = TRUE
			file_name   = ""
		end if
	end if
	if (i_step = 3) then
		if (b_switch = FALSE) then
			if (c_character = Chr(13)) then	' If vbCr = 0D
				' Replace Windows forbidden filename characters
				' file_name = Replace(file_name,".","․",1,-1,0)		' .	002E => ․	2024	' Or .	FF0E
				file_name = Replace(file_name,"/","/",1,-1,0)		' /	002F => /	FF0F	' Or ⁄	2044
				file_name = Replace(file_name,"\","\",1,-1,0)		' \	005C => \	FF3C	' Or ∖	2216
				file_name = Replace(file_name,":",":",1,-1,0)		' :	003A => :	FF1A	' Or ∶	2236
				file_name = Replace(file_name,"*","*",1,-1,0)		' *	002A => *	FF0A	' Or ∗	2217
				file_name = Replace(file_name,"?","?",1,-1,0)		' ?	003F => ?	FF1F	' Or ‽	203D
				file_name = Replace(file_name,"""","”",1,-1,0)		' "	0022 => ”	2036	' Or "	FF02
				file_name = Replace(file_name,"<","<",1,-1,0)		' <	003C => <	FF1C	' Or ‹	2039
				file_name = Replace(file_name,">",">",1,-1,0)		' >	003E => >	FF1E	' Or ›	203A
				file_name = Replace(file_name,"|","ǀ",1,-1,0)		' |	007C => ǀ	01C0	' Or |	FF5C
				i_step = 2
			else
				file_name = file_name & c_character
			end if
		end if
		b_switch = FALSE
	end if

	if (i_step = 2) then
		if (c_character = Mid(string_end,i_count,1)) then
			i_count = i_count + 1
		else
			i_count = 1
			i_step  = 2
			if (c_character = Mid(string_end,i_count,1)) then
				i_count = i_count + 1
			end if
		end if
		if (i_count >= Len(string_end) + 1) then
			i_count = 1
			i_step  = 4
		end if
	end if
	if (i_step = 4) then
		output_file.Close
		if (file_name <> "") then
			if (file_system_object.FileExists(folder_name & "/" & LCase(file_name) & ".vcf") = FALSE) then
				file_system_object.MoveFile folder_name & "/" & standard_name_formated & ".vcf", folder_name & "/" & file_name & ".vcf"
			end if
		end if
		file_name = ""
		i_step = 0
	end if

Loop
input_file.Close

WScript.Echo end_message & i_count_file

WScript.Sleep 5000
WScript.Quit(0)




______________________________________________________

Voici le second script : il fusionne.
Il prend tous les fichiers ".vcf" qui sont dans son dossier et il crée un fichier appelée "_Contacts.vcf".

----------------------
This is the second script: it merges.
It takes all ".vcf" files that are in its folder and creates a file called "_Contacts.vcf".
----------------------



file_name       = "_Contacts"
error_message_1 = "Erreur :"
'                 "Error:"
error_message_2 = "Le fichier de destination existe déjà." & vbCrLf & "Veuillez renommer ou supprimer le fichier existant."
'                 "The destination file already exists."   & vbCrLf & "Please rename or remove the existing file."
string_end      = "END:VCARD"

'__________________________________________________________________________________________________

dim file_system_object
set file_system_object = CreateObject("Scripting.FileSystemObject")
dim current_directory
set current_directory  = file_system_object.GetFolder(".")
dim list_of_files
set list_of_files      = current_directory.Files

if (file_system_object.FileExists(LCase(file_name)) = TRUE) then
	WScript.Echo(error_message_1 & " """ & file_name & """" & vbCrLf & error_message_2)
	WScript.Sleep 5000
	WScript.Quit(-1)
end if
if (file_system_object.FileExists(LCase(file_name) & ".txt") = TRUE) then
	WScript.Echo(error_message_1 & " """ & file_name & ".txt""" & vbCrLf & error_message_2)
	WScript.Sleep 5000
	WScript.Quit(-2)
end if
if (file_system_object.FileExists(LCase(file_name) & ".vcf") = TRUE) then
	WScript.Echo(error_message_1 & " """ & file_name & ".vcf""" & vbCrLf & error_message_2)
	WScript.Sleep 5000
	WScript.Quit(-3)
end if

dim output_file
set output_file        = file_system_object.OpenTextFile(file_name & ".txt", 2, TRUE, TristateFalse)	' 2 = ForWriting     TristateFalse = ASCII
dim input_file

b_do_not_write   = FALSE
b_switch         = FALSE
i_step           = 0
i_count          = 1
i_loop           = 1
string_extension = ""
c_character      = ""

For Each searched_file In list_of_files
		string_extension = file_system_object.GetExtensionName(searched_file)
		if (LCase(string_extension) = "vcf") then
			' WScript.Echo i_loop & vbTab & searched_file.Size & vbTab & searched_file.Name

			set input_file = file_system_object.OpenTextFile(searched_file.Name, 1, FALSE, TristateUseDefault)	' 1 = ForReading
			Do Until input_file.AtEndOfStream
				c_character = input_file.Read(1)

				if (i_step = 0) then
					if (c_character = Mid(string_end,i_count,1)) then
						i_count = i_count + 1
					else
						i_count = 1
						i_step  = 0
						if (c_character = Mid(string_end,i_count,1)) then
							i_count = i_count + 1
						end if
					end if
					if (i_count >= Len(string_end) + 1) then
						i_count  = 1
						i_step   = 1
						b_switch = TRUE
					end if
				end if
				if (i_step = 1) then
					if (b_switch = FALSE) then
						if (c_character = Chr(13)) then	' If vbCr = 0D
							i_step = 2
							b_do_not_write = TRUE
						else
							i_step = 0
						end if
					end if
					b_switch = FALSE
				end if
				if (i_step = 2) then
					if (c_character = Chr(10)) then	' If vbLf = 0A
						b_do_not_write = TRUE
						i_step = 1
					end if
				end if

				if (b_do_not_write = FALSE) then
					output_file.Write(c_character)
				end if
				b_do_not_write = FALSE
			Loop
			output_file.Write(vbCrLf)
			input_file.Close
			i_loop = i_loop + 1
		end if
Next
output_file.Close

file_system_object.MoveFile file_name & ".txt", file_name & ".vcf"

WScript.Sleep 5000
WScript.Quit(0)



Alors bon, oui, je sais, c'est du VBScript, j'entends déjà les critiques...
- Ça manque totalement d'élégance !
- C'est vieux ! Totalement has-been !
- C'est plus maintenu par Microsoft !
- C'est trop mal compilé ce langage !
- xPTDR gr0s n00B !
- Y'a le powershell !
- Mieux => Le bash run sous doz 10 Anniversary update & Nux !
- En C !
- En MASM !
- En brainfuck ! (Y'a un compilateur en BF ?)
- Et bla et bla et bla bla bla...

Bref, ça reste quand même bien pratique...

Ah oui, j'oubliais, le code tourne en mode fenêtre et en mode console, juste comme ça...
A voir également: