How to end LOOP at this situation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Q. April
Posts: 19
Joined: 27 Jan 2018 14:36
Location: Mayatopia

How to end LOOP at this situation

#1 Post by Q. April » 23 Mar 2018 11:01

I create this simple function because it really useful for me .
(I don't care if someone already create this function or not ,because i try to make it myself). even my coding still bad. :oops:

Q1.How to end LOOP at this situation without make others TEST not worked ?

Output from test 3 :
head=wxyz
body=stuv
tail=opqr
head body tail4=klmn
head body tail5=ghij
head body tail6=cdef
head body tail7=abcd
output what i need :
head=wxyz
body=stuv
tail=opqr
Loop ended when no variable name to set. (same like test 6)

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

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: How to end LOOP at this situation

#2 Post by pieh-ejdsch » 25 Mar 2018 06:22

Your function should be divided into several sub-areas.
Recognition of parameters:
- 0 setting for a delimiter. (default = space)
- 1 setting for creating N variables. (default = max)
- 2 setting for creating variables according to string length (default = InputVar = max)
- 3 setting for dividing the string into N parts. (default = variable length / N parts = 1)
- 4 setting to assign the string to N new variables and to 0, to 1 (default = N), to 2, to 3.
Determine string length
Better allocation of suffixes of the variables eg. _01 - _10 etc.

Your parameter handling is a bit illogical. It should be able to be queried with simple switches and overwrite the default settings if specified.

Q. April
Posts: 19
Joined: 27 Jan 2018 14:36
Location: Mayatopia

Re: How to end LOOP at this situation

#3 Post by Q. April » 26 Mar 2018 08:08

Thanks pieh-ejdsch for your reply .Actually my function just for personal use ,that why i doest really care about "Recognition of parameters" and not name a subject related to function. But even i doest really care about it, i already divided parameter into several sub-areas but not completely done.

Code: Select all

Split by group/multiply:

parameter 1

str/var/s

  (str) string to split 
  
   just  Set  (str) By using original variable name, Output Start From %str1%
  
 (var) variable name to Create from Process, Output Start From %var1% 
        Set (str/var) By using original variable name, Output Start From %str1% 
  
  (s)switch to set from variable name to variable string name
     Set (str/var/s) to set from variable name to variable string name

parameter 2

N or /N

 Group/multiply  by  Number
        1 to Maximun String lenght ( Group by Number )
        /1 to Maximun String lenght ( multiply by Number )


 split by Delimeter :

parameter 1
 str var d . dlo     
 
(str) string to split      

parameter 2

 (var) variable name to Create from Process, Output Start From %var1% 
        set parameter 2 (o) By using original variable name, Output Start From %str1%   

parameter 3

(d) switch for split by Delimiter

parameter 4

(.) delimiter character to process

parameter 5

 Set (dlo) for delete original variable 

I really want to make it like your suggested ,but i have fix a problem first like Q1,Q2.Q3

(Thanks again pieh-ejdsch and really sorry my English not very well.)

Or Maybe i should give up to create this function

Q. April
Posts: 19
Joined: 27 Jan 2018 14:36
Location: Mayatopia

Re: How to end LOOP at this situation

#4 Post by Q. April » 03 Apr 2018 08:31

.....DONE......

VARIABLE SPLITTER

Test Vars.bat

Code: Select all


@echo off & call Vars.bat /

::goto skip

echo.
::..Test 1...................................................
echo. Split By Digits
set "mystr=abcdefghijklmnopqrstuvwxyz"
%v% mystr 2  
set mystr

echo.
::..Test 2...................................................
echo. Set Variable Name As Output Name
set "mystr=abcdefghijklmnopqrstuvwxyz"
%v% mystr/myvar 3  
set myvar

echo.
::..Test 3...................................................
echo. Set Costom Variable Name As Output Name
set "myvar=head body tail"
set "mystr=abcdefghijklmnopqrstuvwxyz"
%v% mystr/myvar/s 4  
set head
set body
set tail
echo.


::..Test 4...................................................
echo. Split by divide
set "mystr=abcdefghijklnmnopqrstuvwxyz"
%v% mystr/mmyvar m/3 
set mmyvar


::..Test 5...................................................
echo.
echo. Split by Delimeter .
set "domain=com.org.onion.wix"
echo.
%v% domain d/.  

set domain 
echo ............................................


::..Test 6...................................................
echo.Test 6. Split by Delimeter  
set "domain=com.org.onion.wix"
echo.
%v% domain/myward d/.  

set myward 
echo ............................................

:skip
echo.
::..Test 7...................................................
echo. Split by Delimeter and Set Costom Variable Name As Output Name

set "wrd=Create Kill Repeat"
set "varn=Head Body Tail"
echo. 
%v% wrd/varn/str d    
echo. %Head%,%Body%,%Tail%
pause

goto :eof
Vars.bat

Code: Select all

@echo off
:: Create Environment Variable if parameter 1 set to / ( call Vars to %v% )
if "%~1"=="/" endlocal & set "v=call Vars" & goto end
:: Setting Help Menu and Make a function to display Help menu if clicked
set "svs=%~1" & if not defined svs set ".=echo. "^
 & color 0c & title Variable Spliter ... . & goto helpvars
:: define Environment Variable
if not defined Count goto start
goto startvars

:helpvars 


%.%..............................................................................   
%.%
%.% 1.( Split by group/multiply/Delimeter )
%.%   
%.%    Syntax : call vars str  
%.%    Syntax : call vars str/var  
%.%    Syntax : call vars str/var/s  
%.%
%.%   # Parameter 1 ( str/var/s )
%.% 
%.%   (str) Par_1.1 to split 
%.%     
%.%   just Set (str) By using original variable name, Output Start From str1
%.%     
%.%   (var) variable name to Create from Process, Output Start From var1 
%.%           Set (str/var) By using original variable name, 
%.%           Output Start From str1
%.%     
%.%    (s) Switch to set from variable name to variable string name
%.%        Set (str/var/s) to set from variable name to variable string name
%.%..............................................................................   
%.%
%.%   # Parameter 2 ( N or m/N or d/.)
%.%   
%.%   split by Group/Number
%.%
%.%       N
%.%
%.%      (N) 1 to Maximun string lenght 
%.%           Example :call vars str 3
%.%           
%.%   split by Multiply  
%.%
%.%        m/N
%.%     
%.%       (m) Switch for split by Multiply
%.%       (N) 1 to Maximun string lenght 
%.%
%.%       Example :call vars str m/3
%.%      
%.%
%.%   split by Delimeter  
%.%
%.%         d/.
%.%
%.%       (d) Switch for split by Delimeter
%.%       (.) delimiter character to process  
%.%
%.%       Example :call vars str d/.
%.%..............................................................................   
%.%
%.%   # parameter 3
%.%   
%.%       (dlo) Switch for delete original variable 
%.%       (bck) Switch for processing from back 
%.%.............................................................................. 
%.% 
%.%            .. .  Environment Variable . ..  
%.% 
%.%                       sl,Count,Grp  
%.%      
%.% 
%.%... Variable Spliter v 0.01 ...................................... Q .....
%.% 
%.%

pause & goto end

:startvars

:: Reset Environment Variable

set "sl="
set "Grp="
set "Count="

:start


:varsloop

setlocal enabledelayedexpansion
set /A Count+=1

:: Define Parameter 1
:dp
set "Par_1=%~1"
for /f "tokens=1-3 delims=/" %%a in ("%Par_1%") do (call :ps %%a,%%b,%%c)
goto dp2

:ps
set "Par_1.1=!%~1!"
set "Par_1.2=%~2"
set "Par_1.3=%~3"

if "%3"=="s" (
set "costom=true"
set "cvarname=!%Par_1.2%!"
)

if "%3"=="s" for /f "tokens=%Count% delims= " %%a IN ("%cvarname%") DO ( 
             set "Par_1.2=%%a"
    )


:endskipc
goto :eof

:: Define Parameter 2
:dp2
set "Par_2=%2"
for /f "tokens=1-3 delims=/" %%a in ("%Par_2%") do (call :ps2 %%a,%%b,%%c)
if "%Par_2.1%"=="d" goto dm
set "Par_3=%~3" & if defined Par_3 goto dp3
goto startprc

:ps2
set "Par_2.1=%1"
set "Par_2.2=%2"
if not defined sl call :strLen Par_1.1 sl
if "%1"=="m" set /A "Par_2.1=%sl% / %2"

goto :eof

:: Define Parameter 3
:dp3
for /f "tokens=1-3 delims=/" %%a in ("%Par_3%") do (call :ps2 %%a,%%b,%%c)
:ps2
set "Par_2.1=%1"
set "Par_2.2=%2"

goto :eof



::................................................................................::
::................................................................................::

:startprc

:: Par_1.1(String) Par_1.2(Variable Name)
:: Par_2.1(Group) Par_2.2(Process)
if not defined Par_1.2 set "Par_1.2=%1" 
if not defined sl call :strLen Par_1.1 sl
if defined Grp set /A Grp+=%Par_2.1%
if not defined Grp set Grp=%Par_2.1%

set Par_2.2=!Par_1.1:~-%Grp%,%Par_2.1%!

if defined costom goto skipprc1

for /f "tokens=1-3 delims=," %%A in ("%Par_2.2%,%Par_1.2%,%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 ("%Par_2.2%,%Par_1.2%") 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
setlocal enabledelayedexpansion
:dm
:: 
:: Par_1.1(String) Par_1.2(Variable Name) Par_1.3(switch)
:: Par_2.1(Switch) Par_2.2(Delimeter)
:dmloop

set "str=%Par_1.1%"
set "varname=%Par_1.2%"
set "dlm=%Par_2.2%"

if not defined varname set "varname=%1"

if not defined dlm set "dlm= "
if "%Par_1.3%"=="str" goto prcdlm2

FOR /f "tokens=%Count% delims=%dlm%" %%a IN ("%str%") DO ( set "proc=%%a" )

if not defined proc goto end

for /f "tokens=1,2,3 delims=," %%A in ("%proc%,%varname%,%Count%") do (
    (endlocal & set "%%B%%C=%%A" & set Count=%Count% 
)
)

goto varsloop

:prcdlm2

set "varname=!%varname%!"
if not defined dlm set "dlm= "


FOR /f "tokens=%Count% delims= " %%a IN ("%varname%") DO ( 
set "varn=%%a" 
)

FOR /f "tokens=%Count% delims=%dlm%" %%a IN ("%str%") DO ( set "proc=%%a" )

if not defined proc goto end


for /f "tokens=1,2,3 delims=," %%A in ("%proc%,%varn%,%Count%") do (
   (endlocal & set "%%B=%%A" & set Count=%Count% 
)
)

goto varsloop


:.............................................................:
:: Get string lenght Function
: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 "%~3"=="dlo" endlocal & set "%1="

goto :eof

Thanks

Post Reply