views:

563

answers:

2

Suppose I have a string "AAA BBB CCC DDD EEE FFF".

How can I split the string and retrieve the nth substring, in a batch file?

The equivalent in C# would be

"AAA BBB CCC DDD EEE FFF".Split()[n]


EDIT
Thanks for the helpful answers. In the end I decided that this was trying to make a batchfile perform unnatural acts, so I went with powershell.

+2  A: 

see HELP FOR and see the examples

or quick try this

 for /F %%a in ("AAA BBB CCC DDD EEE FFF") do echo %%c
PA
Thanks, this definitely works. I was hoping to be able to parameterize it, so I could get the nth string, not ... the 3rd string (%%c). or the 4th string (%%d).
Cheeso
A: 

you can use vbscript instead of batch(cmd.exe)

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
str1 = objArgs(0)
s=Split(str1," ")
For i=LBound(s) To UBound(s)
    WScript.Echo s(i)
    WScript.Echo s(9) ' get the 10th element
Next

usage:

c:\test> cscript /nologo test.vbs "AAA BBB CCC"
ghostdog74