(view source code of ldif2vcard.bat as plain text)
@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Check command line arguments
IF "%~1"=="" GOTO Syntax
IF /I NOT "%~x1"==".ldif" GOTO Syntax
IF NOT EXIST "%~1" GOTO Syntax
IF NOT "%~2"=="*" IF /I NOT "%~x2"==".vcf" GOTO Syntax
:: use local variables
SETLOCAL ENABLEDELAYEDEXPANSION
IF "%~2"=="" (
SET vcfFile="%~dpn1.vcf"
) ELSE (
SET vcfFile="%~f2"
)
:: Use 8.3 notation for LDIF file to prevent doublequotes in FOR /F argument
SET ldifFile=%~fs1
:: Clear the variables used in the "outer" FOR loop
CALL :Clear
FOR /F "tokens=*" %%A IN (%ldifFile%) DO (
REM dn: cn=... always is the first line of an address "block", so when
REM we encounter it, the previous one is complete and can be processed
ECHO.%%A | FINDSTR /R /I /B /C:"dn: *cn=" >NUL && (
REM Very basic progress monitoring
TITLE %%A
REM Check if an address was loaded already, i.e. skip processing the first time we find this dn: line
SET vCard. 2>&1 | FIND /I "vCard." >NUL && (
REM Skip groups and mailing lists, we will only export persons
IF "!person!"=="1" (
REM More basic progress monitoring
SET vCard.
ECHO.
REM If %2 was * then we'll save each addres in its own individual vCard file,
REM using the person's name for file name; if not we'll use a single vCard file
IF "%~2"=="*" SET vcfFile="%~dp0!vCard.cn!.vcf"
REM Start writing the vCard
>> !vcfFile! ECHO BEGIN:VCARD
>> !vcfFile! ECHO VERSION:2.1
>> !vcfFile! ECHO N:!vCard.sn!;!vCard.givenName!;;;
>> !vcfFile! ECHO FN:!vCard.cn!
IF NOT "!vCard.telephoneNumber!"=="" (
>> !vcfFile! ECHO TEL;WORK:!vCard.telephoneNumber!
)
IF NOT "!vCard.homePhone!"=="" (
>> !vcfFile! ECHO TEL;HOME:!vCard.homePhone!
)
IF NOT "!vCard.mobile!"=="" (
>> !vcfFile! ECHO TEL;CELL:!vCard.mobile!
)
>> !vcfFile! ECHO EMAIL;PREF:!vCard.mail!
IF NOT "!vCard.mozillaSecondEmail!"=="" (
>> !vcfFile! ECHO EMAIL;HOME:!vCard.mozillaSecondEmail!
)
IF NOT "!!vCard.street!!vCard.l!!vCard.postalCode!"=="" (
>> !vcfFile! ECHO ADR;WORK:;;!vCard.street!;!vCard.l!;;!vCard.postalCode!;
)
IF NOT "!!vCard.mozillaHomeStreet!!vCard.mozillaHomeLocalityName!!vCard.mozillaHomePostalCode!"=="" (
>> !vcfFile! ECHO ADR;HOME:;;!vCard.mozillaHomeStreet!;!vCard.mozillaHomeLocalityName!;;!vCard.mozillaHomePostalCode!;
)
>> !vcfFile! ECHO END:VCARD
REM Clear file name variable or add an empty line, depending on %2
IF "%~2"=="*" (SET vcfFile=) ELSE (>> !vcfFile! ECHO.)
)
)
REM Clear the variables again
CALL :Clear
)
REM Check if the address block is a person's address
ECHO.%%A | FINDSTR /R /I /B /C:"objectclass: *person" >NUL && SET person=1
REM Save each value from the LDIF file in a variable
FOR /F "tokens=1* delims=: " %%B IN ("%%~A") DO (
FOR %%D IN (cn description givenName homePhone mail mobile mozillaCustom1 mozillaCustom2 mozillaNickname mozillaSecondEmail sn telephoneNumber mozillaHomeLocalityName mozillaHomePostalCode mozillaHomeStreet street l postalCode) DO (
IF /I "%%~B"=="%%~D" (SET vCard.%%D=%%C)
)
)
)
ENDLOCAL
GOTO:EOF
:Clear
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=)
SET person=0
GOTO:EOF
:Syntax
ECHO.
ECHO LDIF2vCard.bat, Version 0.11 alfa
ECHO Very basic LDIF to vCard converter
ECHO.
IF NOT "%OS%"=="Windows_NT" ECHO Usage: LDIF2VCARD input.ldif
IF NOT "%OS%"=="Windows_NT" ECHO or: LDIF2VCARD input.ldif output.vcf
IF NOT "%OS%"=="Windows_NT" ECHO or: LDIF2VCARD input.ldif *
IF "%OS%"=="Windows_NT" ECHO Usage: LDIF2VCARD input.ldif [ output.vcf ^| * ]
ECHO.
ECHO Notes: When * is used for the second argument, each address will be
ECHO written to its own individual file, named after the person.
ECHO If no second argument is given, the output will be written to
ECHO a single *.vcf file, named after the input file.
ECHO This batch file has hardly been tested, use it at your own risk.
ECHO.
ECHO Known issues: Batch file fails on ampersands in the LDIF file; a workaround
ECHO would be to "escape" these with carets first, and remove the
IF NOT "%OS%"=="Windows_NT" ECHO carets again from input as well as output file(s) afterwards.
IF "%OS%"=="Windows_NT" ECHO carets again from input as well as output file^(s^) afterwards,
IF "%OS%"=="Windows_NT" ECHO i.e. temporarily replace ^& with ^^^&
ECHO Not all possible fields are included; feel free to modify this
ECHO batch file to make it meet your requirements.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
IF "%OS%"=="Windows_NT" EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0069 seconds