:x3 set a=!a:~%b%,-1!& set /a b=0 :x4 if not defined a goto x8 if !a:~%b%^,1!==!d! set /a n=0 & goto x6 :x5 if not !a:~%b%^,1!==!e! set /a b+=1 & goto x5 set s=!s!!d!!a:~0,%b%!!d!!e!& set /a b+=1 set a=!a:~%b%!& set /a b=0 & goto x4 :x6 if !a:~%b%^,1!==!d! set /a n+=1 if !n! equ 2 set /a b+=1 & goto x7 set /a b+=1 & goto x6 :x7 set s=!s!!a:~0,%b%!!e!& set /a b+=1 set a=!a:~%b%!& set /a b=0 & goto x4 :x8 if defined s set s=!s:~0,-1! endlocal & set %1=%s%