Page 1 of 1

split string

Posted: 23 Feb 2007 01:39
by lcd11001
Please fix my error !!!!

REM ~~~~~~~~~~~~
REM count.bat
REM ~~~~~~~~~~~~

@echo off

set info=false

REM default ch=space and str=null
set ch=
set str=

set /a count=0
set /a len=0

if not "%1"=="" set str=%1
if not "%2"=="" set ch=%2
if not "%3"=="" set info=%3

set left=
set right=

:loop

if "%left%"=="%str%" goto end

set left=%str%

set cmd=set left=%%left:~0,%len%%%
call %cmd%

set right=%left%
set cmd=set right=%%right:~-1%%
if not "%left%"=="" call %cmd%

if "%right%"=="%ch%" set /A count=count+1

set /A len=len+1

goto loop

:end
if %info%==true (
echo str=%str%
echo ch=%ch%
echo len=%len%
echo count=%count%
)


REM ~~~~~~~~~~~~
REM split.bat
REM ~~~~~~~~~~~~


@echo off

set str=abc:123:zz:7:8:9
set ch=:

FOR /F "tokens=1,2,3,4,5,6 delims=:" %%i IN ("%str%") DO (
echo. %%i & echo. %%j & echo. %%k & echo. %%l & echo. %%m & echo. %%n
)

call count.bat %str% %ch%
set /a count=count + 1

FOR /L %%c IN (1,1,%count%) DO (
echo %%c

:arrow: Error here :evil:
REM FOR /F "tokens=%c% delims=:" %%i IN ("%str%") DO echo %%i
)


You can copy & paste to create count.bat & split.bat
Then, run split.bat in cmd.

Posted: 26 Feb 2007 00:08
by DosItHelp
lcd11001,

The FOR variable %%c doesn't resolve within the FOR option. Optionally use a batch function like this:

Code: Select all

REM ~~~~~~~~~~~~
REM split.bat
REM ~~~~~~~~~~~~

@echo off

set str=abc:123:zz:7:8:9
set ch=:

FOR /F "tokens=1,2,3,4,5,6 delims=:" %%i IN ("%str%") DO (
    echo. %%i & echo. %%j & echo. %%k & echo. %%l & echo. %%m & echo. %%n
)

call count.bat %str% %ch%
set /a count=count + 1

FOR /L %%c IN (1,1,%count%) DO (
    echo %%c
    call:printtoken %%c : "%str%"
)
GOTO:EOF


:printtoken -- counts the number of occurrences of needle in haystack
::          -- pos    [in] - position
::          -- delim  [in] - delimiter
::          -- string [in] - string to parse
:$source http://www.dostips.com
:printtoken
FOR /F "tokens=%~1 delims=%~2" %%i IN (%3) DO echo.%~1:%%i
GOTO:EOF


You can also simplify the count code with a count function like shown below. The improved :printtoken function also allows empty tokens, i.e. two delimiters with nothing in between:

Code: Select all

REM ~~~~~~~~~~~~
REM split.bat
REM ~~~~~~~~~~~~

@echo off

set str=abc::123:zz:7:8:9
set ch=:

FOR /F "tokens=1,2,3,4,5,6 delims=:" %%i IN ("%str%") DO (
    echo. %%i & echo. %%j & echo. %%k & echo. %%l & echo. %%m & echo. %%n
)

call:count %ch% "%str%" count
set /a "count+=1"

FOR /L %%c IN (1,1,%count%) DO (
    call:printtoken %%c : "%str%"
)
GOTO:EOF


:count needle haystack ret -- counts the number of occurrences of needle in haystack
::                         -- needle   [in]  - string to find
::                         -- haystack [in]  - string to search in
::                         -- ret      [out] - valref for return value
:$source http://www.dostips.com
SETLOCAL
set "haystack=%~2"
set /a "ret=0"
FOR /f %%a in ('"(echo.%%haystack:%~1=&echo.%%)|find /c /v """') DO set /a "ret=%%a-1"
( ENDLOCAL & REM RETURN VALUES
    IF "%~3" NEQ "" (SET %~3=%ret%) ELSE ECHO.%ret%
)
GOTO:EOF


:printtoken -- counts the number of occurrences of needle in haystack
::          -- pos    [in] - position
::          -- delim  [in] - delimmiter
::          -- string [in] - string to parse
:$source http://www.dostips.com
:printtoken
SETLOCAL
set "str=%~3"
for /f "tokens=1,* delims=[]" %%a in ('"(echo.%%str:%~2=&echo.%%)|find /v /n """') do (
    if %%a==%~1 echo.%~1: %%b
)
GOTO:EOF


Hope this helps :wink: