Batch Script to parse lines in text file
Hello,
I'm trying to write a batch script that performs actions on each value
within a text file. The text file (myfile.txt) contains a string
similar to:
aa\bb\cc\dd\ee
My goal is to have a for loop that perfoms a set of actions on each of
these values individually. So just trying to get it to echo these
variables, I'm able to get it to echo either the first value in the
string or just the entire line. So I'm trying something like:
for /f "tokens=1 delims=\" %%i in (myfile.txt) do echo %%i
Now I'm sure I could add separate tokens and echo statements for %j %k
%l and %m, but this just won't work for me in the long run as the
number of values in myfile.txt can be anywhere between 1 and 1000
values. If anyone can help me figure this out, I would greatly
appreciate it.
Regards,
John
Re: Batch Script to parse lines in text file
On Jan 23, 10:00 am, jnton...@gmail.com wrote:
> Hello,
>
> I'm trying to write a batch script that performs actions on each value
> within a text file. The text file (myfile.txt) contains a string
> similar to:
>
> aa\bb\cc\dd\ee
>
> My goal is to have a for loop that perfoms a set of actions on each of
> these values individually. So just trying to get it to echo these
> variables, I'm able to get it to echo either the first value in the
> string or just the entire line. So I'm trying something like:
>
> for /f "tokens=1 delims=\" %%i in (myfile.txt) do echo %%i
>
> Now I'm sure I could add separate tokens and echo statements for %j %k
> %l and %m, but this just won't work for me in the long run as the
> number of values in myfile.txt can be anywhere between 1 and 1000
> values. If anyone can help me figure this out, I would greatly
> appreciate it.
>
> Regards,
> John
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/
Re: Batch Script to parse lines in text file
> 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.
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.
Re: Batch Script to parse lines in text file
<jntoner1@gmail.com> wrote in message
news:e354cdd9-22f5-4049-85d3-b0b15047a108@a39g2000prl.googlegroups.com...
> 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
call:process %_zz%
pause & goto:eof
:process
set/a _c = 0
:processloop
if "%1" NEQ "" (
set/a _c += 1
echo/processing item !_c!: %1
shift
goto:processloop
)
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
Re: Batch Script to parse lines in text file
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
> call:process %_zz%
> pause & goto:eof
>
> :process
>
> set/a _c = 0
>
> :processloop
> if "%1" NEQ "" (
> set/a _c += 1
> echo/processing item !_c!: %1
> shift
> goto:processloop
> )
> 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!
Re: Batch Script to parse lines in text file
<jntoner1@gmail.com> wrote in message
news:3a0b38a4-527b-4e5f-a094-d7fb9f4ce08b@i18g2000prf.googlegroups.com...
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
> call:process %_zz%
> pause & goto:eof
>
> :process
>
> set/a _c = 0
>
> :processloop
> if "%1" NEQ "" (
> set/a _c += 1
> echo/processing item !_c!: %1
> shift
> goto:processloop
> )
> 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
call:process %_zz%
pause & goto:eof
:process
set/a _c = 0
:processloop
if "%~1" NEQ "" (
set/a _c += 1
echo/processing item !_c!: [%~1]
shift
goto:processloop
)
goto:eof
/Al
Re: Batch Script to parse lines in text file
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
call:process %_zz%
goto:eof
:process
set/a _c = 0
:processloop
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
goto:processloop
)
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:
"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?
Thanks for the help.
Re: Batch Script to parse lines in text file
<jntoner1@gmail.com> wrote in message
news:7000e04b-2ed0-4473-a8e6-734875f38978@l16g2000yqo.googlegroups.com...
> 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
> call:process %_zz%
>
> goto:eof
>
> :process
>
> set/a _c = 0
>
> :processloop
> 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
> goto:processloop
> )
> 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
Re: Batch Script to parse lines in text file
On Jan 27, 5:51 pm, "Al Dunbar" <aland...@hotmail.com> wrote:
> <jnton...@gmail.com> wrote in message
>
> news:7000e04b-2ed0-4473-a8e6-734875f38978@l16g2000yqo.googlegroups.com...
>
>
>
>
>
> > 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
> > call:process %_zz%
>
> > goto:eof
>
> > :process
>
> > set/a _c = 0
>
> > :processloop
> > 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
> > goto:processloop
> > )
> > 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- Hide quoted text -
>
> - Show quoted text -
You know, I swear that I had tested that previously and I thought it
gave me a different result...I guess not. Thanks again for the help!