(I don't care if someone already create this function or not ,because i try to make it myself). even my coding still bad.
Q1.How to end LOOP at this situation without make others TEST not worked ?
Output from test 3 :
output what i need :head=wxyz
body=stuv
tail=opqr
head body tail4=klmn
head body tail5=ghij
head body tail6=cdef
head body tail7=abcd
Loop ended when no variable name to set. (same like test 6)head=wxyz
body=stuv
tail=opqr
Variable Splitter:
Code: Select all
@echo off
goto skip
echo.
::..Test 1...................................................
echo. Split By Digits
set "mystr=abcdefghijklmnopqrstuvwxyz"
call :vars mystr 2
set mystr
echo.
::..Test 2...................................................
echo. Set Variable Name As Output Name
set "mystr=abcdefghijklmnopqrstuvwxyz"
call :vars mystr/myvar 3
set myvar
:skip
echo.
::..Test 3...................................................
echo. Set Costom Variable Name As Output Name
set "myvar=head body tail"
set "mystr=abcdefghijklmnopqrstuvwxyz"
call :vars mystr/myvar/s 4
set head
set body
set tail
echo.
::..Test 4...................................................
echo. Split by divide
set "mystr=abcdefghijklnmnopqrstuvwxyz"
call :vars mystr/mmyvar m/3
set mmyvar
echo.
::..Test 5...................................................
echo. Split by Delimeter and delete original variable
set "domain=com.org.onion.wix"
echo.
call :vars domain O d . dlo
set domain
echo.
::..Test 6...................................................
echo. Split by Delimeter and Set Costom Variable Name As Output Name
set "varname=Head Body Tail"
set "wrd=Create Kill Repeat"
echo.
call :vars wrd varname d
echo. %Head%,%Body%,%Tail%
pause
goto :eof
::........................Funtion..Begin.........................::
:vars
@echo off
if not defined Count goto start
:: Reset Environment Variable
set "sl="
set "Grp="
set "Count="
:start
if "%3"=="d" goto dm
:varsloop
setlocal enabledelayedexpansion
:: Define Parameter 1
:dp
set "str=%1"
for /f "tokens=1-3 delims=/" %%a in ("%str%") do (call :ps %%a,%%b,%%c)
goto dp2
:ps
set "String=!%1!"
set "varname=%2"
set /A Count+=1
if "%3"=="s" set "varname=!%varname%!"
if "%3"=="s" for /f "tokens=%Count% delims= " %%a IN ("%varname%") DO (
set "varname=%%a"
set "costom=%%a"
)
if not defined varname set "varname=%1"
goto :eof
:: Define Parameter 2
:dp2
set "dp2=%2"
for /f "tokens=1-3 delims=/" %%a in ("%dp2%") do (call :ps2 %%a,%%b,%%c)
goto startprc
:ps2
set "Group=%1"
set "prc=%2"
if not defined sl call :strLen string sl
if "%1"=="m" set /A "Group=%sl% / %2"
goto :eof
:startprc
if not defined sl call :strLen string sl
if defined Grp set /A Grp+=%Group%
if not defined Grp set Grp=%Group%
::set /A Count+=1
set prc=!string:~-%Grp%,%Group%!
::echo %costom%
if defined costom goto skipprc1
for /f "tokens=1-3 delims=," %%A in ("%prc%,%varname%,%Count%") do (
(endlocal & set "%%B%%C=%%A" & set "Count=%Count%" & set "sl=%sl%" & set "Grp=%Grp%"
)
)
goto endskip
:skipprc1
for /f "tokens=1-2 delims=," %%A in ("%prc%,%varname%") do (
(endlocal & set "%%B=%%A" & set "Count=%Count%" & set "sl=%sl%" & set "Grp=%Grp%"
)
)
:endskip
if %grp% geq %sl% goto end
goto varsloop
::Split By Delimeter
:dm
:dmloop
setlocal enabledelayedexpansion
set "str=!%1!"
set "var=%2"
set "dlm=%4"
if "%2"=="varname" set "varn=!%2!"
if not defined dlm set "dlm= "
if "%2"=="o" set "var=%1"
if "%2"=="O" set "var=%1"
set /A Count+=1
if "%2"=="varname" FOR /f "tokens=%Count% delims= " %%a IN ("%varn%") DO (
set "varn=%%a"
)
FOR /f "tokens=%Count% delims=%dlm%" %%a IN ("%str%") DO ( set "proc=%%a" )
if not defined proc goto end
if "%2"=="varname" for /f "tokens=1,2,3 delims=," %%A in ("%proc%,%varn%,%Count%") do (
(endlocal & set "%%B=%%A" & set Count=%Count%
)
goto dmloop
)
for /f "tokens=1,2,3 delims=," %%A in ("%proc%,%var%,%Count%") do (
(endlocal & set "%%B%%C=%%A" & set Count=%Count%
)
goto dmloop
)
:.............................................................:
:strLen
setlocal enabledelayedexpansion
:strLen_Loop
if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
:end
:: delete original Variable
if "%5"=="dlo" endlocal & set "%1="
goto :eof
Q2.Any best ways to END LOCAL at Dynamic Function ?
Q3.how to make TEST 5 able to process Delimeter like character(&)?
Thanks