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

Batch Scripts - string parsing

Reply to Message Icon

Original Message
Name: lklektau
Date: May 12, 2005 at 07:40:57 Pacific
Subject: Batch Scripts - string parsing
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


Report Offensive Message For Removal

Response Number 1
Name: IVO
Date: May 12, 2005 at 09:21:25 Pacific
Subject: Batch Scripts - string parsing
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 2
Name: lklektau
Date: May 12, 2005 at 11:27:41 Pacific
Subject: Batch Scripts - string parsing
Reply: (edit)

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?


Report Offensive Follow Up For Removal

Response Number 3
Name: lklektau
Date: May 12, 2005 at 12:48:03 Pacific
Subject: Batch Scripts - string parsing
Reply: (edit)

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!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch Scripts - string parsing

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software