1.

Solve : Batch file to split a string into multiple strings?

Answer»

Hi. I need a script that will take a string and split it up by " %% " with no quotes. It would be very easy if you could output them on different lines (displayed).

Thank you in advance.Please explain a bit more what you want, and preferably give an example of input and desired output.
I want so that when I input "Hello %% Person %%"(no quotes) , it would give me the following:
Hello
PersonWill there always be 2 names?
No. There is no way to tell.Is it possible to use VBScript?
I would prefer to have them as one file, but it is ok to use it.

Also, the input needs to come from the batch file.Batch script that creates and runs VBscript in same folder

Code: [Select]
@echo off
echo MyString=wscript.arguments(0)>splitter.vbs
echo MyArray = Split(MyString, "%%%%", -1, 1)>>splitter.vbs
echo last=UBound(MyArray)>>splitter.vbs
echo For j = 0 To last>>splitter.vbs
echo wscript.echo MyArray(j)>>splitter.vbs
echo Next>>splitter.vbs

REM Testing example string
REM note that a % SIGN is a control character in batch so needs to be escaped
REM by another % sign (i.e. doubled)

set Longstring=Cat%%%%Dog%%%%Horse%%%%BIRD%%%%Fish%%%%Chicken

for /f "delims=" %%A in ( 'cscript //nologo splitter.vbs "%Longstring%" ' ) do echo %%A


REM at end of batch (if desired)
DEL splitter.vbs
Thank you, but it just says "Echo is off".Quote from: flytothemoon on June 11, 2010, 01:41:07 PM

Thank you, but it just says "Echo is off".

I ran this batch file...

Code: [Select]@echo off
echo MyString=wscript.arguments(0)>splitter.vbs
echo MyArray = Split(MyString, "%%%%", -1, 1)>>splitter.vbs
echo last=UBound(MyArray)>>splitter.vbs
echo For j = 0 To last>>splitter.vbs
echo wscript.echo MyArray(j)>>splitter.vbs
echo Next>>splitter.vbs
set Longstring=Cat%%%%Dog%%%%Horse%%%%Bird%%%%Fish%%%%Chicken
echo Longstring=%Longstring%
for /f "delims=" %%A in ( 'cscript //nologo splitter.vbs "%Longstring%" ' ) do echo %%A
del splitter.vbs

and this was the output...

Code: [Select]Longstring=Cat%%Dog%%Horse%%Bird%%Fish%%Chicken
Cat
Dog
Horse
Bird
Fish
Chicken

If you will show me the code you have used I will try to advise. Where are you getting the long string from?


It changed on me. Now it is working. But one problem. I am getting the longstring from a file that I know exists (I entered the path in windows explorer) but it says the file is not found. Any ideas?Quote from: flytothemoon on June 11, 2010, 02:22:11 PM
Any ideas?

Be thorough and careful and check what you are doing.


Discussion

No Comment Found