Computing.Net > Forums > Programming > Batch Scripts - string parsing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch Scripts - string parsing

Reply to Message Icon

Name: lklektau
Date: May 12, 2005 at 07:40:57 Pacific
OS: WinXP
CPU/Ram: n/a
Comment:

Hi,

Is there any way for a for loop to parse each word in a string one by one?

For instance,

For /F %%i in ('findstr /c:"TestString" file.txt') do echo %%i

will only echo the first word of each line. If I put in "tokens=*" then it gives me the whole line at once...I want each word in the line one at a time, and I don't want to hardcode what token number the string is at or how many words are on each line.

Thanks for any help,

-Lila



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: May 12, 2005 at 09:21:25 Pacific
Reply:

The following returns each word in a For controlled loop

For /F "tokens=*" %%I in ('FindStr /C:"TestString" File.txt') Do Call :PARSE %%I
....
....
GoTo :EOF

:PARSE
If "%1"=="" GoTo :EOF
Echo %1
Shift
GoTo :PARSE

Where :EOF is the standard built in return label for XP/2k/NT batch scripts and does not need to be declared and :PARSE is an internal subroutine.


0

Response Number 2
Name: lklektau
Date: May 12, 2005 at 11:27:41 Pacific
Reply:

Thanks for the help! Unfortunately I guess my case isn't that simple. The line I'm actually trying to parse is from an xml file, and looks something like this:

<Product UpgradeCode="CC0107D0-E535-4D7D-9EF4-82C9F07F2AD6" Name="my installer" Id="F60BBF4F-E3D9-41E1-88CB-0798D0EC1A4E" Version="2.0.0.1" Manufacturer="my work" Language="1033">

I need to pick out the Version attribute. When I try and adapt the code, for one thing the < and > characters cause problems. When I set tokens=2* to get past the <, the only arguments that seem to be passed to :PARSE are UpgradeCode and "CC0107D0-E535-4D7D-9EF4-82C9F07F2AD6".

I was hoping there was a routine that would just loop me through every word tokenized by white space. Any ideas?


0

Response Number 3
Name: lklektau
Date: May 12, 2005 at 12:48:03 Pacific
Reply:

looking through, even white space wouldn't work. I've put together something with a lot of different calls and got it to work, but thanks for your help!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Please help! Intel assembler



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch Scripts - string parsing

Batch script string parsing www.computing.net/answers/programming/batch-script-string-parsing/18461.html

batch file string parsing www.computing.net/answers/programming/batch-file-string-parsing/16603.html

Batch Scripting-String Extraction www.computing.net/answers/programming/batch-scriptingstring-extraction/14471.html