Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find string file 1

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
What is the code to find a specific string in a textfile and return the text next to this string ?

example text

[Vector]
flStageXPosition = -24056000
flStageYPosition = -15476000
SpecimenRotation = 2000000
SpecimenTilt = 0
Magnification = 50.000
HighTension = 5000.000
FWD = 10.075


I would like to find the string "SpecimenRotation = " and the script returning with "2000000
 
Search for reading INI files, [Vector] is a section in the INI file and SpecimenRotation is a setting.
 
In the following file, "c:\test.ini":

--------------------
[a]
b=c
--------------------

"c" is returned by the following:

c = System.PrivateProfileString("c:\test.ini", "a", "b")

This works in Word VBA.
 
Or, to put it more specifically...

If you have a file c:\zzz\Test.ini, and it contains:

[Vector]
flStageXPosition = -24056000
flStageYPosition = -15476000
SpecimenRotation = 2000000
SpecimenTilt = 0
Magnification = 50.000
HighTension = 5000.000
FWD = 10.075


then the code:
Code:
Sub GetINI_Data()
MsgBox System.PrivateProfileString("c:\zzz\Test.ini", _
   "Vector", "SpecimenRotation")
End Sub
returns a messagebox with "2000000". Look up PrivateProfileString in Help. The syntax is:

expression.PrivateProfileString(FileName, Section, Key)

In the example above:

Filename = c:\zzz\Test.ini
Section = Vector
Key = SpecimenRotation

The returned value is the value of the key SpecimenRotation.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks for your help, This works fine !
But I have a new challenge for you....

In the file there is also a piece of text not specified by a Section (see example).

***coded text***

Date=11/26/2008
Time=10:25:46 AM
User=supervisor
UserText=sample 12a
UserTextUnicode=5


[Vector]
flStageXPosition = -24056000
flStageYPosition = -15476000
SpecimenRotation = 2000000
SpecimenTilt = 0
Magnification = 50.000
HighTension = 5000.000
FWD = 10.075


Is it also possible to extract the UserText ?
 

If you deal with just 'regular' text file, you can always do:
Code:
Open "c:\zzz\Test.ini" For Input As #1
Do While Not EOF(1)   
   Line Input #1, strTextLine  
   If Left$(strTextLine, 9) = "UserText=" Then
      MsgBox Mid$(strTextLine, 10)
      Exit Do
   End If
Loop
Close #1

Have fun.

---- Andy
 
My solution would be to insist on a section name, why make life difficult ;-)

If this really is not an option then Andrzejek's solution is fine, as long as "UserText=" occurs before any sections that also contain it.

Paul Rigby
VBA - C# - Delphi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top