Link to home
Start Free TrialLog in
Avatar of nivo_Z
nivo_Z

asked on

Windows batch script - string parsing

HI,

I'm writing a windows batch script (.bat).
I have the following string:
"\item1\item2\item3\" - the number of items may change. The string is enclosed with \ as in the example.
I want to retrieve the last item, how do I do that?

Thanks,

Nivo
Avatar of harris_c
harris_c
Flag of Philippines image

Very hard using batch file (.bat).

Consider using python or perl?


hec",)
Avatar of nivo_Z
nivo_Z

ASKER

I wouldn't be here asking for help if I could use any script language I want.
It must be .bat.
ASKER CERTIFIED SOLUTION
Avatar of harris_c
harris_c
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nivo_Z

ASKER

it's a good idea to switch the / to newline, but how??
Avatar of nivo_Z

ASKER

GOT IT!!

Your answer gave me an idea.
tokens=1* means that %%b gets all the string except the first token, so using goto I parse each time the remaining until the string is empty, leaving me with the last token.
UUPPPI

SET string=a\b\c\d\nivo

:BB
      set current=%string%
      FOR  /F "tokens=1* delims==\" %%a IN ("%string%") DO (
      SET string=%%b
      )

if NOT "%string%"=="" (goto BB)
@echo %current%
Nice one nivo and Thanks.

But I am just curious why you need it in a bat ^_^


hec",)
Avatar of nivo_Z

ASKER

I'm a developer, working on a product that runs on Windows.
I have a small task that we don't want write in C/Java since it involves many system calls that are easy for a batch.
The way we run the product today, customers don't need any scripting tool or 3rd parties (execpt of course Java which is very common and obvious), just Windows OS.
We don't want to change that because of a small, not so very important, feature.

Thanks again,

Nivo