Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ldif2vcard.bat

(view source code of ldif2vcard.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: Check command line arguments
  5. IF "%~1"=="" GOTO Syntax
  6. IF /I NOT "%~x1"==".ldif" GOTO Syntax
  7. IF NOT EXIST "%~1" GOTO Syntax
  8. IF NOT "%~2"=="*" IF /I NOT "%~x2"==".vcf" GOTO Syntax
  9.  
  10. :: use local variables
  11. SETLOCAL ENABLEDELAYEDEXPANSION
  12. IF "%~2"=="" (
  13. 	SET vcfFile="%~dpn1.vcf"
  14. ) ELSE (
  15. 	SET vcfFile="%~f2"
  16. )
  17. :: Use 8.3 notation for LDIF file to prevent doublequotes in FOR /F argument
  18. SET ldifFile=%~fs1
  19.  
  20. :: Clear the variables used in the "outer" FOR loop
  21. CALL :Clear
  22.  
  23. FOR /F "tokens=*" %%A IN (%ldifFile%) DO (
  24. 	REM dn: cn=... always is the first line of an address "block", so when
  25. 	REM we encounter it, the previous one is complete and can be processed
  26. 	ECHO.%%A | FINDSTR /R /I /B /C:"dn: *cn=" >NUL && (
  27. 		REM Very basic progress monitoring
  28. 		TITLE %%A
  29. 		REM Check if an address was loaded already, i.e. skip processing the first time we find this dn: line
  30. 		SET vCard. 2>&1 | FIND /I "vCard." >NUL && (
  31. 			REM Skip groups and mailing lists, we will only export persons
  32. 			IF "!person!"=="1" (
  33. 				REM More basic progress monitoring
  34. 				SET vCard.
  35. 				ECHO.
  36. 				REM If %2 was * then we'll save each addres in its own individual vCard file,
  37. 				REM using the person's name for file name; if not we'll use a single vCard file
  38. 				IF "%~2"=="*" SET vcfFile="%~dp0!vCard.cn!.vcf"
  39. 				REM Start writing the vCard
  40. 				>> !vcfFile! ECHO BEGIN:VCARD
  41. 				>> !vcfFile! ECHO VERSION:2.1
  42. 				>> !vcfFile! ECHO N:!vCard.sn!;!vCard.givenName!;;;
  43. 				>> !vcfFile! ECHO FN:!vCard.cn!
  44. 				IF NOT "!vCard.telephoneNumber!"=="" (
  45. 					>> !vcfFile! ECHO TEL;WORK:!vCard.telephoneNumber!
  46. 				)
  47. 				IF NOT "!vCard.homePhone!"=="" (
  48. 					>> !vcfFile! ECHO TEL;HOME:!vCard.homePhone!
  49. 				)
  50. 				IF NOT "!vCard.mobile!"=="" (
  51. 					>> !vcfFile! ECHO TEL;CELL:!vCard.mobile!
  52. 				)
  53. 				>> !vcfFile! ECHO EMAIL;PREF:!vCard.mail!
  54. 				IF NOT "!vCard.mozillaSecondEmail!"=="" (
  55. 					>> !vcfFile! ECHO EMAIL;HOME:!vCard.mozillaSecondEmail!
  56. 				)
  57. 				IF NOT "!!vCard.street!!vCard.l!!vCard.postalCode!"=="" (
  58. 					>> !vcfFile! ECHO ADR;WORK:;;!vCard.street!;!vCard.l!;;!vCard.postalCode!;
  59. 				)
  60. 				IF NOT "!!vCard.mozillaHomeStreet!!vCard.mozillaHomeLocalityName!!vCard.mozillaHomePostalCode!"=="" (
  61. 					>> !vcfFile! ECHO ADR;HOME:;;!vCard.mozillaHomeStreet!;!vCard.mozillaHomeLocalityName!;;!vCard.mozillaHomePostalCode!;
  62. 				)
  63. 				>> !vcfFile! ECHO END:VCARD
  64. 				REM Clear file name variable or add an empty line, depending on %2
  65. 				IF "%~2"=="*" (SET vcfFile=) ELSE (>> !vcfFile! ECHO.)
  66. 			)
  67. 		)
  68. 		REM Clear the variables again
  69. 		CALL :Clear
  70. 	)
  71. 	REM Check if the address block is a person's address
  72. 	ECHO.%%A | FINDSTR /R /I /B /C:"objectclass: *person" >NUL && SET person=1
  73. 	REM Save each value from the LDIF file in a variable
  74. 	FOR /F "tokens=1* delims=: " %%B IN ("%%~A") DO (
  75. 		FOR %%D IN (cn description givenName homePhone mail mobile mozillaCustom1 mozillaCustom2 mozillaNickname mozillaSecondEmail sn telephoneNumber mozillaHomeLocalityName mozillaHomePostalCode mozillaHomeStreet street l postalCode) DO (
  76. 			IF /I "%%~B"=="%%~D" (SET vCard.%%D=%%C)
  77. 		)
  78. 	)
  79. )
  80. ENDLOCAL
  81. GOTO:EOF
  82.  
  83.  
  84. :Clear
  85. FOR %%B IN (cn description givenName homePhone mail mobile mozillaCustom1 mozillaCustom2 mozillaNickname mozillaSecondEmail sn telephoneNumber mozillaHomeLocalityName mozillaHomePostalCode mozillaHomeStreet street l postalCode) DO (SET vCard.%%B=)
  86. SET person=0
  87. GOTO:EOF
  88.  
  89.  
  90. :Syntax
  91. ECHO.
  92. ECHO LDIF2vCard.bat,  Version 0.11 alfa
  93. ECHO Very basic LDIF to vCard converter
  94. ECHO.
  95. IF NOT "%OS%"=="Windows_NT" ECHO Usage:         LDIF2VCARD  input.ldif
  96. IF NOT "%OS%"=="Windows_NT" ECHO    or:         LDIF2VCARD  input.ldif  output.vcf
  97. IF NOT "%OS%"=="Windows_NT" ECHO    or:         LDIF2VCARD  input.ldif  *
  98. IF     "%OS%"=="Windows_NT" ECHO Usage:         LDIF2VCARD  input.ldif  [ output.vcf ^| * ]
  99. ECHO.
  100. ECHO Notes:         When * is used for the second argument, each address will be
  101. ECHO                written to its own individual file, named after the person.
  102. ECHO                If no second argument is given, the output will be written to
  103. ECHO                a single *.vcf file, named after the input file.
  104. ECHO                This batch file has hardly been tested, use it at your own risk.
  105. ECHO.
  106. ECHO Known issues:  Batch file fails on ampersands in the LDIF file; a workaround
  107. ECHO                would be to "escape" these with carets first, and remove the
  108. IF NOT "%OS%"=="Windows_NT" ECHO                carets again from input as well as output file(s) afterwards.
  109. IF     "%OS%"=="Windows_NT" ECHO                carets again from input as well as output file^(s^) afterwards,
  110. IF     "%OS%"=="Windows_NT" ECHO                i.e. temporarily replace ^& with ^^^&
  111. ECHO                Not all possible fields are included; feel free to modify this
  112. ECHO                batch file to make it meet your requirements.
  113. ECHO.
  114. ECHO Written by Rob van der Woude
  115. ECHO http://www.robvanderwoude.com
  116.  
  117. IF "%OS%"=="Windows_NT" EXIT /B 1
  118.  

page last modified: 2024-04-16; loaded in 0.0069 seconds