C++/VB - Read value in txt file

Asked By bcastain
08-Feb-10 04:41 AM
Hello to everybody

I would like to read a value in a txt file. For example, the value
will following the egual sign. So I want to look for language and get


language = fr
country = france

Thank you by advance and have a nice day.
ObjFSO.OpenTextFile
(1)
Match.Value
(1)
ObjRegExp.Execute
(1)
ObjFile.ReadAll
(1)
Match.length
(1)
IntFindLine
(1)
ObjFile.Close
(1)
IgnoreCase
(1)
  Pegasus [MVP] replied to bcastain
08-Feb-10 06:27 AM
The Split function is your answer:
sString = "language = fr"
aString = split(sString, "=")
wscript.echo trim(aString(2))
  bcastain replied to Pegasus [MVP]
08-Feb-10 11:59 AM
a93ca9221025@w16g2000yqw.googlegroups.com...

Thank you very much for your quick answer.
But my text file will have many lines, and I would like to find a
value for each parameter I look for.
For example, if I look for language, I want to get fr, If I look for
country, I will have france ....

Thanks
  mayayana replied to bcastain
08-Feb-10 01:54 PM
If you have it set up like an INI file
see here:

http://www.jsware.net/jsware/scripts.php5#classpk

In the download is a class that you can paste
into your script. It will let you treat an INI
file like an object.

Otherwise, you can use Split to divide
your file into an array of lines before proceeding.
Pegasus was giving you the basic idea, but
you cannot expect someone to write your script
for you. If you do not know how to open the file
or handle arrays, etc. then you need to download
the WSH help file:

http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-
8A76-1C4099D7BBB9


--
--
.

Thank you very much for your quick answer.
But my text file will have many lines, and I would like to find a
value for each parameter I look for.
For example, if I look for language, I want to get fr, If I look for
country, I will have france ....

Thanks
  LikeToCode replied to bcastain
08-Feb-10 01:55 PM
This can get you started. strString would be the variable you charged from
either a ReadAll() or ReadLine() method. This will produce the value you are
looking for but you can also tweak this to count the number of occurrences of
your search term in the whole file, etc.

Dim strString, strSearch, intLength
strSearch = InputBox ("Enter your Search Term")
If strSearch <> "" Then
strString = "country = france"
intLength = Len(strSearch) + 2
MsgBox RegExp(strString,strSearch)
End If

Function RegExp(Strng,Patrn)
Dim objRegExp, Matches, Match, intCount
Set objRegExp = New RegExp
With objRegExp
.Pattern = "\b" & Patrn & "\b.*$"
.IgnoreCase = True
.Global = True
End With
Set Matches = objRegExp.Execute(Strng)
For Each Match In Matches
intCount = Match.length - intLength
RegExp = Right(Match.Value, intCount)
Next
End Function
  Al Dunbar replied to bcastain
08-Feb-10 11:27 PM
Then, perhaps that is what you should have asked for, rather than just
saying "I would like to read a value in a text file". Pegasus showed you how
to do precisely that.


What would you expect to get for the country if this text was present in the
file:

country = france
country = germany

Perhaps if you explained what the file is and what it means, it would be
easier for someone to provide an answer that meets your unstated
expectations.


/Al
  bcastain replied to Al Dunbar
11-Feb-10 05:05 PM
.com...
t
how
the

Hello everybody,

Thanks to you I could manage my code, you are right, pegasus put me on
the track with the split function.
My need is to read in a text file different value that will be used to
configure an application

the txt file will look

country =3D france
language =3D fr
info1 =3D value
....


So now with the split function I can put all the string in an array
and look for the wanted value.


Set objFile =3D objFSO.OpenTextFile("\\c:\script\param.txt", ForReading)
strContents =3D objFile.ReadAll
objFile.Close
arrWords =3D Split(strContents, vbcrlf)
intStart =3D 0
IntFindLine =3D 0
For Each strWord in arrWords
If InStr(strWord, value) Then
intStart =3D intStart + 1
End If

If intStart > 0 Then
intFindLine =3D IntFindLine + 1
strLine =3D strLine & strWord
if intFindLine > 0 then exit For
End If
Next
arrWords =3D Split(strLine, " =3D ")
strValue =3D (arrWords(1))
Wscript.Echo strValue
help
errors out on line 13. Right at the this point it errors out Set objTextFile = objFSO.OpenTextFile _ (" \ " & strComputer & "c$ \ csg \ wccad \ wccad.exe run VOICET WIN_32", ForReading) Can anyone help? Set 2 Const adOpenStatic = 3 Const adLockReadOnly = 1 strComputer = "." Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ (" \ " & strComputer & "c$ \ csg \ wccad \ wccad.exe run VOICET WIN_32", ForReading) Do Until objTextFile.AtEndOfStream 54.1" & vbCrLf Else strNewText = strNewText & strLine & VbCrLf End If Loop objTextFile.Close Set objTextFile = objFSO.OpenTextFile _ (" \ " & strComputer & " \ c$ \ csg \ wccad \ wccad.exe -run VOICET WIN_32", ForWriting) objTextFile.Write(strNewText) objTextFile of the C$ part. To be a valid UNC, it should read . . . Set objTextFile = 3D objFSO.OpenTextFile _ (" \ " & strComputer & _ Plus, that final WShell.run line with no arguments makes no sense
the following statements, Const ForReading = 1, ForWriting = 2 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C: \ Textfile.txt", ForWriting, False) The "Textfile.txt" goes blank(it has some text) if be able to write at the same time. Thnkz AS VBScript Discussions Scripting.FileSystemObject (1) ObjFSO.OpenTextFile (1) Database (1) ObjFSO (1) CreateObject (1) ObjFile (1) Thnkz (1) It is not possible
What does the 1 do at the end? This works also without it. Set objTextFileComps = objFSO.OpenTextFile("clients.txt", 1) Thanks -Will VBScript Discussions OpenTextFile (1) ObjTextFileComps (1) FamilyId (1) WillRe (1 What does the 1 do at the end This works also without it. Set objTextFileComps = objFSO.OpenTextFile(clients.txt, 1) Thanks -Will?