Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Batch Script to parse lines in text file

Reply
Thread Tools Display Modes

Re: Batch Script to parse lines in text file

 
 
Al Dunbar
Guest
Posts: n/a

 
      01-23-2009

<> wrote in message
news:e354cdd9-22f5-4049-85d3-...
> It's a lot easier to do this in a WSH script, but since you seem
> comfortable in batch try this hybrid script ...
>
> @echo off
> echo wsh.echo Replace(wsh.stdin.readall, "\", vbnewline) > %temp%
> \tmp.vbs
> cscript //nologo %temp%\tmp.vbs < myfile.txt > outfile.txt
> del %temp%\tmp.vbs
>
> HTH,
>
> Tom Lavedas
> ***********http://there.is.no.more/tglbatch/- Hide quoted text -
>
> - Show quoted text -


I'm not opposed to using WSH...I just don't know WSH. This wsh script
providded doesn't appear to get me what I need...this spits out my
text in a different formatted text file, which isn't what I'm looking
for.

===> Your only stated requirement was to "perform a set of actions on each
of these values individually". Tom illustrated one example, leaving the
specific details up to you to code according to the specs you had not yet
shared.

My ultimate goal is to query a registry key, which lists GUID values
of other registry keys and then take this list of GUIDs and translate
these into "friendly" names. I'm querying a key in the registy for the
"Contains" value, which lists several GUIDs...Here's my script:

rem HKLM\Software\Company\Group\GUID\Contains REG_MULTI_SZ GUID1 GUID2
GUID3..


for /f "tokens=3 skip=2 delims=\ " %%i in ('reg query HKLM\Software
\Company\Group\GUID /v Contains') do set guidlist=%%i
for /f "tokens=2* skip=1" %%a in ('reg query HKLM\Software\Company\
%guidlist% /v Name') do set friendlyname=%%b
for /f "tokens=2* skip=1" %%c in ('reg query HKLM\Software\Company\
%guidlist% /v Version') do set version=%%d
echo %frienlyname% %version%

This works fine collecting the first GUID value in this group, but I
can't figure out how to loop this to call the other 100+ values in
this "Contains" key. I figured I'd try writing these GUIDs out to a
temp file and then loop thru this file, but I'm getting the same
result...only calls the first value in the file and won't loop to the
2nd+ value.

===> Here is another partial solution, but I hope it will help you
successfully get around the FOR command token limits:

@echo off

setlocal enabledelayedexpansion
pushd "%~DP0"


rem get the list of "/"-delimited items from a file into a variable...
for /f %%X in (ZZ.TXT) do (set _zz=%%X)

rem convert the "/" delimiters to blank characters
(set _zz=%_zz:/= %)

rem pass the list of items to an internal subroutine
callrocess %_zz%
pause & goto:eof

rocess

set/a _c = 0

rocessloop
if "%1" NEQ "" (
set/a _c += 1
echo/processing item !_c!: %1
shift
gotorocessloop
)
goto:eof

The zz.txt file contains a single line with text strings separated by "/"
characters. In my test this file was about 5800 bytes in size and contained
455 items, and each was displayed accurately.

/Al


 
Reply With Quote
 
 
 
 
Al Dunbar
Guest
Posts: n/a

 
      01-23-2009

<> wrote in message
news:3a0b38a4-527b-4e5f-a094-...
On Jan 23, 2:12 pm, "Al Dunbar" <aland...@hotmail.com> wrote:
> I'm not opposed to using WSH...I just don't know WSH. This wsh script
> providded doesn't appear to get me what I need...this spits out my
> text in a different formatted text file, which isn't what I'm looking
> for.
>
> ===> Your only stated requirement was to "perform a set of actions on each
> of these values individually". Tom illustrated one example, leaving the
> specific details up to you to code according to the specs you had not yet
> shared.
>
> My ultimate goal is to query a registry key, which lists GUID values
> of other registry keys and then take this list of GUIDs and translate
> these into "friendly" names. I'm querying a key in the registy for the
> "Contains" value, which lists several GUIDs...Here's my script:
>
> rem HKLM\Software\Company\Group\GUID\Contains REG_MULTI_SZ GUID1 GUID2
> GUID3..
>
> for /f "tokens=3 skip=2 delims=\ " %%i in ('reg query HKLM\Software
> \Company\Group\GUID /v Contains') do set guidlist=%%i
> for /f "tokens=2* skip=1" %%a in ('reg query HKLM\Software\Company\
> %guidlist% /v Name') do set friendlyname=%%b
> for /f "tokens=2* skip=1" %%c in ('reg query HKLM\Software\Company\
> %guidlist% /v Version') do set version=%%d
> echo %frienlyname% %version%
>
> This works fine collecting the first GUID value in this group, but I
> can't figure out how to loop this to call the other 100+ values in
> this "Contains" key. I figured I'd try writing these GUIDs out to a
> temp file and then loop thru this file, but I'm getting the same
> result...only calls the first value in the file and won't loop to the
> 2nd+ value.
>
> ===> Here is another partial solution, but I hope it will help you
> successfully get around the FOR command token limits:
>
> @echo off
>
> setlocal enabledelayedexpansion
> pushd "%~DP0"
>
> rem get the list of "/"-delimited items from a file into a variable...
> for /f %%X in (ZZ.TXT) do (set _zz=%%X)
>
> rem convert the "/" delimiters to blank characters
> (set _zz=%_zz:/= %)
>
> rem pass the list of items to an internal subroutine
> callrocess %_zz%
> pause & goto:eof
>
> rocess
>
> set/a _c = 0
>
> rocessloop
> if "%1" NEQ "" (
> set/a _c += 1
> echo/processing item !_c!: %1
> shift
> gotorocessloop
> )
> goto:eof
>
> The zz.txt file contains a single line with text strings separated by "/"
> characters. In my test this file was about 5800 bytes in size and
> contained
> 455 items, and each was displayed accurately.
>
> /Al


This is perfect. Thank you very much!

===> You're welcome.

I forgot to mention that this assumes that none of the "items" contain any
embedded whitespace. In the event that some do, below is a modification of
the method that will take care of that in most simple cases. Note the use of
the <delims=> keyword in the FOR command, the change to the set command that
does the textual substitution (it now replaces </> with <" ">), and the use
of <%~1> in the internal routine, which strips the leading and trailing
double-quotes from the parameter.

@echo off

setlocal enabledelayedexpansion
pushd "%~DP0"

rem get the list of "/"-delimited items from a file into a variable...
for /f "delims=" %%X in (zz.txt) do (set _zz=%%X)
set _

rem convert the "/" delimiters to blank characters
(set _zz="%_zz:/=" "%")
set _

rem pass the list of items to an internal subroutine
callrocess %_zz%
pause & goto:eof

rocess

set/a _c = 0

rocessloop
if "%~1" NEQ "" (
set/a _c += 1
echo/processing item !_c!: [%~1]
shift
gotorocessloop
)
goto:eof


/Al


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      01-27-2009

<> wrote in message
news:7000e04b-2ed0-4473-a8e6-...
> Ok, this is "almost" working...I'm just having an issue trying to find
> the last piece. I'm using your example, but the processloop isn't
> echoing the results quite right. So here's what I am doing:
>
> I've got a file named MyFile.txt which contains:
>
> aa\bb\cc\dd\ee
>
> My script contains the following:
>
> setlocal enabledelayedexpansion
> pushd "%~DP0"
>
> rem get the list of "\"-delimited items from a file into a variable...
> for /f %%X in (MyFile.txt) do set _zz=%%X
>
> rem convert the "/" delimiters to blank characters
> (set _zz=%_zz:\0= %)
>
> rem pass the list of items to an internal subroutine
> callrocess %_zz%
>
> goto:eof
>
> rocess
>
> set/a _c = 0
>
> rocessloop
> if "%1" NEQ "" (
> set/a _c += 1
>
> for /f "tokens=2* skip=1" %%a in ('reg query HKLM\Software\Company\%1 /
> v Name') do set friendlyname=%%b
> for /f "tokens=2* skip=1" %%c in ('reg query HKLM\Software\Company\%1 /
> v Version') do set version=%%d
>
> echo %frienlyname% %version%
>
> shift
> gotorocessloop
> )
> goto:eof
>
>
> My results of my echo are a little bit off. Seems like the first time
> thru the loop, it isn't echoing the values of %frienlyname% %version
> %...instead, it is just giving:


Please review my first reply, and note that in my example, I used "!"
instead of "%" to emit the current value of a variable. A sequence of
commands contained within parentheses is called a compound command. Before
the first command is executed, all instances of %variable% references are
expanded to the values the variables have when the compound command is first
parsed.

Try this:

@echo off
setlocal enabledelayedexpansion
(set _V=000)
for %%F in (AA BB CC) do (
(set _V=%%F)
echo/%%F us %_V%? or is it !_V!?
)
pause

> "ECHO is off."
>
> Then it loops thru the rest of my file, skipping the last entry. Seems
> like the "set" inside the "if" loop doesn't take effect until the next
> run thru the loop. How do I make the "do set %frienlyname% %version%"
> values take effect immediately and echo these values in this loop?


use !frienlyname! !version! instead.

/Al


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Commands To Read and Delete Lines from txt-File officer07 Windows Server 3 03-04-2009 06:19 AM
Re: Importing variables into Batch script from text file MMC-Michelle Scripting 2 03-05-2007 03:35 PM
script to read and write 2 lines from text file Kevini Scripting 3 11-14-2006 10:10 AM
Re: Script To Parse A Text File? Sam Low Scripting 0 08-12-2004 10:44 AM
Script To Parse A Text File? sbillard Scripting 0 08-10-2004 09:27 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59