[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Windows batch script - string parsing

Posted on 2006-05-25
7
Medium Priority
?
528 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:nivo_Z
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:harris_c
ID: 16767087
Very hard using batch file (.bat).

Consider using python or perl?


hec",)
0
 

Author Comment

by:nivo_Z
ID: 16767543
I wouldn't be here asking for help if I could use any script language I want.
It must be .bat.
0
 
LVL 8

Accepted Solution

by:
harris_c earned 400 total points
ID: 16770296
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nivo_Z
ID: 16771828
it's a good idea to switch the / to newline, but how??
0
 

Author Comment

by:nivo_Z
ID: 16772164
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
 
LVL 8

Expert Comment

by:harris_c
ID: 16773814
Nice one nivo and Thanks.

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


hec",)
0
 

Author Comment

by:nivo_Z
ID: 16774154
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question