Search code examples
vbscriptcyrillic

Reading and storing cyrillic in variables


I am trying to read cyrillic values separated by ; from a file, scan every line for match in the first value and assign the following values to variables.

Here is the code I have so far:

Dim objStream, strData, splitted
Set objStream = CreateObject("ADODB.Stream")
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile("C:\temp\textfile.txt")
strData = objStream.ReadText()
MsgBox strData
splitted = split(strData, ";") 

textfile.txt contains something like this:

Име;Адрес;Телефон;
Име2;Адрес2;Телефон2;

I will have a variable like this:

searchFor = "Име"

and the script must assign Адрес and Телефон to variables.

Basically, I need to search for the name(Име or Име2) in every line from the textfile and then assign the second and the third values of that line to variables.

Currently strData gets the data but it's stored as a string that I cannot manipulate or don't know how.


Solution

  • First split the text at newlines, then split each line at semicolons, then check if the first field matches your search value. Example:

    searchFor = "Име"
    For Each line In Split(strData, vbNewLine)
      fields = split(line, ";")
      If fields(0) = searchFor Then
        varA = fields(1)
        varB = fields(2)
      End If
    Next
    

    Note that you must save the script in Unicode format, lest your search string be botched.