Sunday, October 27, 2013

CMD DOS BATCH Example of adding a comma delimiter to each character in a string

CMD DOS BATCH  Example of adding a comma delimiter to each character in a string

This example will take a string and add a comma after each character.  This will mek the variable able to be itterated one character at a time as well as count the number of characters, etc.


SendIndividualCharacters.cmd:

echo off
cls
setlocal EnableDelayedExpansion
set myVar=AStringWithNoDelimiters
echo myVar 1 = !myVar!
call :StrToComSep myVar
echo myVar 2 = !myVar!
goto:eof


:StrToComSep
rem This subroutine takes a variable and places a comma after each character
rem and white space to be used as a deliminator
set string=!%1!#
set pos=0
:startLoop
        call set chr=%%string:~%pos%,1%%
        if !chr!==# goto done
        set varRebuild=!varRebuild!!chr!,
        set /a pos+=1
goto startLoop

:done
set varRebuild=!varRebuild:~0,-1!
set "%1=%varRebuild%"
goto:eof


Output from the above example:


myVar 1 = AStringWithNoDelimiters
myVar 2 = A,S,t,r,i,n,g,W,i,t,h,N,o,D,e,l,i,m,i,t,e,r,s





Note:  This example based on example on http://www.computerhope.com/forum/index.php?topic=112779.0;wap

No comments:

Post a Comment