1.

Solve : Using "for" as a seperator?

Answer»

I've been working on an batch script which will take a string and using a for command will separate that string into several other strings and so far this is the best script I got it to work with...

Code: [Select]@echo off
:a
if "%ln%"=="" echo.
if not "%ln%"=="" echo %ln%
echo.
set /P Line=
cls
if "%Line%"=="Done" goto B
set /a TC=%TC%+1
set T=%T%%TC%,
set T%TC%=echo %%%TC%
set Ln=%ln%%Line%-
goto a
:b
echo.
for /f "tokens=%T:~0,-1% delims=-" %%1 in ("%Ln%") do call :c
echo.
pause>nul
exit
:c
set /a NC=%NC%+1
call set Op=%%T%NC%%%
for /f %%a in ("string") do %Op%
if "%NC%"=="%TC%" exit /b
goto c

does ANYONE know a better way to do this or an alternative solution?Could you try explaining, in words, what you are trying to do?
Well I thought I did, but I'll show an example in words.

Take this string...

Hello-my name is...-some random text

This will be split up for where ever there is a "-" a new line will start so it will become

Hello
my name is...
some random text

that is what the above batch script does I'm asking if there is another way to do that, or in other words an alternative way.

EDIT: Also the code above uses the for command in order to separate the string into multiple strings hence the title of the topic. I'm sorry if this is confusing in anyway.@echo off
set /p "Line=Enter your text, with two - in it: "
for /f "tokens=1,2,3 delims=-" %%a in ("%line%") do (
echo %%a
echo %%b
echo %%c
)
pauseArbitrary number of tokens

@echo off

REM Get a string to split
set /p String="Enter a string "

:Loop

REM Split string into 1st TOKEN and the remainder
for /f "tokens=1* delims=-" %%A in ("%String%") do (

REM Echo 1st token
echo %%A

REM Assign remainder to %String%
set String=%%B

)

REM If %String% is a blank then we are finished
if "%String%"=="" goto Done

REM Otherwise go round again
goto Loop

:Done

Result:

C:\>Split-hyphen.bat
Enter a string 1-2-3-4-5-6-7-8-9
1
2
3
4
5
6
7
8
9
C:\>Split-hyphen.bat
Enter a string cat-dog-horse-bird-tree
cat
dog
horse
bird
tree
C:\>Split-hyphen.bat
Enter a string I RAN - I fell - I got up again
I ran
I fell
I got up again
C:\>

Coming soon: Hybrid batch/VBScript





@echo off
if exist SplitString.vbs del SplitString.vbs
>> SplitString.vbs Echo StringToSplit = Wscript.Arguments(0)
>> SplitString.vbs Echo CharToSplit = Wscript.Arguments(1)
>> SplitString.vbs Echo SplitArray = Split (StringToSplit, CharToSplit, -1, 1)
>> SplitString.vbs Echo For j = 0 to Ubound (SplitArray)
>> SplitString.vbs Echo Wscript.Echo SplitArray (j)
>> SplitString.vbs Echo Next
set /p String="String to split? "
set /p SplitC="Char(s) to split on? "
for /f "delims=" %%A in ('cscript //nologo SplitString.vbs "%String%" "%SplitC%"') do echo %%A


C:\>VSplit-hyphen.bat
String to split? 1-2-3-4-5-6-7-8-9
Char(s) to split on? -
1
2
3
4
5
6
7
8
9
C:\>VSplit-hyphen.bat
String to split? cat-dog-horse-bird-tree
Char(s) to split on? -
cat
dog
horse
bird
tree
C:\>VSplit-hyphen.bat
String to split? I ran - I fell - I got up again
Char(s) to split on? -
I ran
I fell
I got up again
C:\>VSplit-hyphen.bat
String to split? apple++Pear++Orange
Char(s) to split on? ++
apple
Pear
Orange
C:\>


Note: VBSCript allows splitting on a string of multiple characters

C:\>VSplit-hyphen.bat
String to split? I ran - I fell - I got up again
Char(s) to split on? - <--- There is a space here
I ran
I fell
I got up again







Nice work.

Here's another simple method of displaying it to the console.

Quote

@echo off
setlocal enabledelayedexpansion
set "var=One, Two-Buckle my shoe.-Three Four,-Walk out the door."
:: keep the blank line in below.
(set LF=^

)
echo %var:-=!LF!%
pause


One, Two
Buckle my shoe.
Three Four,
Walk out the door.
Press any key to continue . . .Thank you all for the replies this has helped me shorten the script I was working on significantly.


Discussion

No Comment Found