1.

Solve : Parsing a string?

Answer»

I am SURE there is a simple method to do this but I am missing it. I want a batch FILE which will take an incoming variable, take it apart and put it together in a little bit different order. I might want to take the date string (Mon 05/26/2008) and reorder it as (2008\05\26). For any difference which it might make, I will be doing this under WinXP CMD. Thanks.you can extract part of a string as follows

Consider the string:

Mary had a little lamb

First of all we put it in a variable. Let's call the variable "string".

set string=Mary had a little lamb

XP command has built in string slicing capability.

echo substring:

echo %string:~A,B%

or assign substring to variable:

set substring=%string:~A,B%

where A and B are LITERAL numbers:

A is the offset from the start (i.e. 0 [zero] is the first character)
B is the number of characters to slice

So,

echo %string:~0,4% will show this:

Mary

and

set newstring=%string~0,4%#####%string:18,4%

will result in the variable newstring having the value

Mary#####lamb

It should be clear to you now how to do what you want.
you FORGOT to MENTION the venerable for loop, with the tokens/delimit for parsing.

@OP. Besides CMD, you can also try learning a bit of vbscript. It has at least more features for parsing and manipulating strings from files.Quote from: ghostdog74 on May 26, 2008, 06:17:57 AM

you forgot to mention the venerable for loop

I didn't forget; I chose not to mention it, because I think it may be off putting to beginners and those seeking a simple concise solution. Of course, if the position of the desired substring(s) is/are not known exactly, and there are identifiable token delimiters, then FOR is very useful. I have noticed that many people seem to find FOR somewhat non-intuitive.

mmcoy, if you hang around this forum, you will soon discover that ghostdog has a habit of jumping into threads where people have asked for a batch solution, and offering them a vbscript solution. I don't think he has shares in vbscript; I suspect he can't help it. I hesitate to criticize him seriously for it, because some of the solutions he offers are very elegant and sometimes better. I think he genuinely feels that vbscript code looks better than NT/XP/2K/Vista command language, which I think he considers to be crude and ugly. That is a matter of opinion, of course. I think everybody knows mine!






Quote from: Dias de verano on May 26, 2008, 07:10:11 AM
Quote from: ghostdog74 on May 26, 2008, 06:17:57 AM
you forgot to mention the venerable for loop

I didn't forget; I chose not to mention it, because I think it may be off putting to beginners and those seeking a simple concise solution. Of course, if the position of the desired substring(s) is/are not known exactly, and there are identifiable token delimiters, then FOR is very useful. I have noticed that many people seem to find FOR somewhat non-intuitive.




i mentioned it, because OP seems to want to manipulate the output of date. Therefore, the for loop with token/delimit may come in handy.The output of date has an unvarying, predictable structure, and i cannot see why one method of slicing it should be preferred to another, but I am willing to be persuaded.


Discussion

No Comment Found