Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm trying to create a batch file that will extract a specific line from a text file into a variable. The line I need is "Num=xxxxx" (the Xs are unknown numbers), but there is another line later in the file that starts off "TheOtherNum=xxxxxxxxx" (these Xs are different numbers that the ones from the original line). Once I can overcome this issue, I just need to grab the line "TimeZone=xxx" into another variable and then echo the Num and TimeZone variables into a log file. Getting my program to differentiate between the two lines (and only grab the first one) is my main problem. The number of lines in the text file may vary as well. Here's what I have so far:
for /f "tokens=2 delims==" %%a in ('type "%location%\ST0*.ini" ^| find "Num="') do (for /f "tokens=2 delims==" %%b in ('type
"%location%\ST0*.ini" ^| find "TimeZone="') do echo %%a - %%b >>Results.txt)This doesn't work, however; it runs through the lengthy number of files, but doesn't export the %%a variable into the text file properly, not to mention the "TheOtherNum" getting pulled occasionally. I'm still figuring out how to properly stack commands together into a single FOR /F statement.

Two problems. First, you've got nested loops. For each matching file in your folder, it will loop again through all matching files. That's not what you want. You only want to iterate through your folder once.
Second, you are not looking for "Num=" at the beginning of a line.
If you use findstr instead of find, it will solve both of your problems. It allows you to use regular expressions, and can search for both strings in one go.
for /f "tokens=2 delims==" %%a in ('findstr /r "^Num= ^TimeZone=" "%location%\ST0*.ini"') do echo %%a
Not exactly what you want, it prints the Num= and Timezone= on separate lines, but you can use the above as a starting point and tweak it.

Awesome! Minor tweaking, but a great start!! I had completely forgotten about the FINDSTR command. You, sir, are a scholar and a gentleman. Thanks!!!! :)

It may help to post a few lines of a couple files.
=====================================
If at first you don't succeed, you're about average.M2

Here are the relevant lines from the file (the whole thing is actually pretty long):
Num=xxxxx
Address1=blah blah
Address2=blah blah
BankName=blah blah
BankAcctNum=1234567890

![]() |
![]() |
![]() |

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