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
nivo_ZAsked:
Who is Participating?
 
harris_cCommented:
OK.  

Here is what I have so far:

@echo off
FOR /F "tokens=3 delims=/" %%a in ("/abc/def/ghi/") do echo %%a

need to have some way of getting token parameter since the number of items may change...

OR

you can reverse the string, as /ghi/def/abc/...



a split and reverse i found here http://student.vub.ac.be/~dvandeun/batcoll.all but the split does not seem to work anymore =(

maybe a tr in dos would do - change all "/" to newlines then read file.. echo only the last read... which will give you the last element.

hec",)
0
 
harris_cCommented:
Very hard using batch file (.bat).

Consider using python or perl?


hec",)
0
 
nivo_ZAuthor Commented:
I wouldn't be here asking for help if I could use any script language I want.
It must be .bat.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
nivo_ZAuthor Commented:
it's a good idea to switch the / to newline, but how??
0
 
nivo_ZAuthor Commented:
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%
0
 
harris_cCommented:
Nice one nivo and Thanks.

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


hec",)
0
 
nivo_ZAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.