I have scripted ParseFullName.bat in an attempt to parse a last name from a full name string.

The syntax for using ParseFullName.bat is:

ParseFullName "Full Name" GivenNamePlus LastName

Where:

<b>"Full Name"</b>   is a quoted string containing the full name, like "Jerold Lee Schulman".

<b>LastName</b>      is a <a href="/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> that will contain the last name.

<b>GivenNamePlus</b> is a <a href="/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> that will contain all but the last name.
The parsing rules, in order of precedence, are:

1. The first sub-string begins GivenNamePlus.

2. The last sub-string is the end of LastName.

3. When a sub-string is identified as part of the LastName, all subsequent sub-strings are part of LastName.

4. A sub-string that contains a comma (,) is part of LastName.

5. A sub-string that contains a period (.) is part of GivenNamePlus.

6. A single character sub-string is part of GivenNamePlus.

7. A sub-string that contains more that 2 characters is part of GivenNamePlus.

ParseFullName.bat contains:

@echo off
if \{%3\}<h1><a name="_echo_Syntax_ParseFullName_Full_Name_GivenNamePlus_LastName_amp_exit_b1_setlocal_set_fn_1_set_fn_fn_set_fn_fn_#_for_f_Tokens_1_13_a_in_echo_fn_do_call_name_a_b_c_d_e_f_g_h_i_j_k_l_m_endlocal_amp_set_2_fmn_amp_set_3_ln_goto_EOF_name_set_fmn_1_set_ln_name1_shift_set_work1_1_if_1_">\{\} @echo Syntax: ParseFullName "Full Name" GivenNamePlus LastName&exit /b1
setlocal
set fn=%1
set fn=%fn:"=%
set fn=%fn:,=#%
for /f "Tokens=1-13" %%a in ('@echo %fn%') do (
 call :name %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m
)
endlocal&set %2=%fmn%&set %3=%ln%
goto :EOF
:name
set fmn=%1
set ln=
:name1
shift
set work1=%1
if \{%1\}</a></h1>\{\} goto err
if \{%2\}<h1><a name="_goto_last1_set_work2_work1_#_if_work1_NEQ_work2_set_work1_work2_amp_goto_last1_set_work2_work1_if_work1_EQU_work2_goto_last0_name2_set_fmn_fmn_1_goto_name1_last0_if_not_3_">\{\} goto last1
set work2=%work1:#=,%
if "%work1%" NEQ "%work2%" set work1=%work2%&goto last1
set work2=%work1:.=%
if "%work1%" EQU "%work2%" goto last0
:name2
set fmn=%fmn% %1
goto name1
:last0
if not \{%3\}</a></h1>\{\} goto name3
if \{%2\}<h1><a name="_II_goto_last1_if_2_">\{II\} goto last1
if \{%2\}</a></h1>\{III\} goto last1
if \{%2\}<h1><a name="_IV_goto_last1_if_2_">\{IV\} goto last1
if \{%2\}</a></h1>\{V\} goto last1
if \{%2\}<h1><a name="_VI_goto_last1_if_2_">\{VI\} goto last1
if \{%2\}</a></h1>\{VII\} goto last1
if \{%2\}<h1><a name="_VIII_goto_last1_name3_if_work1_1_1_EQU_goto_name2_if_work1_2_1_NEQ_goto_name2_last1_set_ln_work1_last2_shift_if_1_">\{VIII\} goto last1
:name3
if "%work1:~1,1%" EQU "" goto :name2
if "%work1:~2,1%" NEQ "" goto :name2
:last1
set ln=%work1%
:last2
shift
if \{%1\}</a></h1>\{\} goto :EOF
set ln=%ln% %1
goto last2
:err
@echo Cannot parse name - %fmn% %ln% %1 %2 %3 %4 %5 %6 %7
set ln=?
endlocal&set %2=%fmn%&set %3=%ln%
exit /b1