Vbscript - Read ini or text file for specific section

I want to save some addresses in a text file, and then read certain parts of the file based on group membership. I made all the accessories for the members of the group, so I do not need help.

But I'm not sure if I should use a plain text file or an INI file?
The fact is that the email addresses are in two or three lines, and I need a line break.
I tried using a simple text file, but I was not able to correctly split the line.

So, are the .ini files preferable?

An INI file might look like this:

  [London]
 Address 1
 Postbox 3245
 58348 London

 [Copenhagen]
 Address 2
 Postbox 2455
 5478347 Copenhagen

I'm not quite sure if this is possible in an INI file, although perhaps I need to name each line as well. OR, I could use a simple text file and search for the word [London], and then read each line until there is a line break. Then save all these lines in a variable that I pass?

How would you guys decide this?

+4
source share
4 answers

I would most likely use a CSV file where each line will represent a country.

Country,Address1,Address2,Address3,Address4 London,Address 1,Postbox 3245,58348 London Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen 

If you can easily identify your data, then you could probably have more descriptive column names (i.e. Street1, Street2, Town, Postcode, etc.).

This file format is also easy to read, as you only read one line of the input file at a time and split it using something like

 aAddress = split(sLine, ",") 

To make it easier to work with you, you can use the dictionary object and use the country as a key and an array as a value

 'sLine should be read from input file' sLine = "Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen" 'Create dictionary for addresses' Set dic = CreateObject("Scripting.Dictionary") 'Split line into array' aAddressParts = Split(sLine, ",") 'Remove the first element of the array' sValues = Mid(sLine, InStr(sLine, ",")+1) aValues = Split(sValues, ",") 'Add new entry into dictionary' dic.Add aAddressParts(0), aValues 'Usage' MsgBox "Address for Copenhagen: " & vbNewLine & _ Join(dic("Copenhagen"), "," & vbNewLine) 

Thanks Maciej

+1
source

I wrote a small VBScript class that processes "real" ini files written with this format:

  [section_name]
 key1 = value1
 key2 = value2

Code for class:

 Class IniFileObject Private m_Data Private Sub Class_Initialize Set m_Data = Server.CreateObject("Scripting.Dictionary") End Sub Private Sub Class_Terminate Dim key If IsObject(m_Data) Then For Each key In m_Data m_Data(key).RemoveAll Set m_Data(key) = Nothing Next m_Data.RemoveAll Set m_Data = Nothing End If End Sub Public Function Init(sFilePath) Dim arrLines, sLine, x Dim sCurSection, oSectionDict Set Init = Me arrLines = GetFileLines(sFilePath) If Not(IsArray(arrLines)) Then Exit Function sCurSection = "" For x = 0 To UBound(arrLines) sLine = Trim(arrLines(x)) If Len(sLine)>0 Then If Left(sLine, 1)="[" Then If Not(HandleSectionLine(sLine, sCurSection)) Then Exit Function Else If Len(sCurSection)=0 Then Err.Raise 1005, "IniFileObject init", "Found value outside any section (" & Server.HTMLEncode(sLine) & ")" Exit Function End If Set oSectionDict = m_Data(sCurSection) If Not(ParseOneLine(sLine, oSectionDict)) Then Exit Function Set m_Data(sCurSection) = oSectionDict End If End If Next End Function Public Property Get ReadValue(section, key) Dim oSectionDict ReadValue = "" If m_Data.Exists(section) Then Set oSectionDict = m_Data(section) If oSectionDict.Exists(key) Then ReadValue = oSectionDict(key) End If End Property Private Function ParseOneLine(ByVal sLine, ByRef oSectionDict) Dim arrTemp, sErrorMsg, sKey sErrorMsg = "" ParseOneLine = True If Left(sLine, 2)="//" Or Left(sLine, 1)="'" Or Left(sLine, 1)="{" Then Exit Function arrTemp = Split(sLine, "=") If UBound(arrTemp)=1 Then sKey = Trim(arrTemp(0)) If (Len(sKey)>0) And (Len(arrTemp(1))>0) Then If Not(oSectionDict.Exists(sKey)) Then oSectionDict.Add sKey, Trim(arrTemp(1)) Else sErrorMsg = "Key already exists" End If Else sErrorMsg = "Empty key or value" End If Else sErrorMsg = "Missing or too much '=' characters" End If Erase arrTemp If Len(sErrorMsg)>0 Then ParseOneLine = False Err.Raise 1006, "IniFileObject Init", "Failed to parse single line (" & Server.HTMLEncode(sLine) & "): " & sErrorMsg End If End Function Private Function HandleSectionLine(ByVal sLine, ByRef sCurSection) HandleSectionLine = False If (Len(sLine)<3) Or (Right(sLine, 1)<>"]") Then Err.Raise 1002, "IniFileObject init", "Invalid line found: " & Server.HTMLEncode(sLine) Exit Function End If sCurSection = Mid(sLine, 2, Len(sLine) - 2) If m_Data.Exists(sCurSection) Then Err.Raise 1003, "IniFileObject init", "Section exists more than once: " & Server.HTMLEncode(sCurSection) Exit Function End If m_Data.Add sCurSection, Server.CreateObject("Scripting.Dictionary") HandleSectionLine = True End Function Private Function GetFileLines(sFilePath) Dim objFSO, oFile Set objFSO = Server.CreateObject("Scripting.FileSystemObject") If Not(objFSO.FileExists(sFilePath)) Then Set objFSO = Nothing Err.Raise 1001, "IniFileObject init", "file path '" & Server.HTMLEncode(sFilePath) & "' does not exist, check permissions" Exit Function End If Set oFile = objFSO.OpenTextFile(sFilePath) GetFileLines = Split(oFile.ReadAll, VBCrLf) oFile.Close Set oFile = Nothing Set objFSO = Nothing End Function End Class 

Usage example:

 Dim filePath, ini filePath = Server.MapPath("config.ini") Set ini = New IniFileObject.Init(filePath) Response.Write("Value for 'Key001': " & ini.ReadValue("MySection", "Key001") & "<br />") Set ini = Nothing 

The code generates various errors, when the file does not exist or contains invalid lines, the errors are largely clear. It can β€œsuppress” errors and not display an error page when using this code when consumed:

 On Error Resume Next Set ini = New IniFileObject.Init(filePath) If Err.Number<>0 Then Response.Write("Error reading ini file") End If On Error Goto 0 If IsObject(ini) Then Response.Write("Value for 'IP001': " & ini.ReadValue("IPaddress", "IP001") & "<br />") Set ini = Nothing End If 
+2
source

You can save addresses on one line and use a special character, such as an underscore, to indicate line breaks. When you read the address, you just need to replace the special character with a line break.

[London]
Address = "Mailbox 3245_58348 London"

[Copenhagen]
Address = "Mailbox 2455_5478347 Copenhagen"

This allows you to store addresses with many lines or without a mailbox line. In my experience, information such as "our addresses always has exactly two lines, and the first is always the mailbox" is often incorrect ...

+1
source

I use a small executable that launches its own api for this: GetPrivateProfileString and WritePrivateProfileString .

The executable is called like this:

 Set sh = CreateObject("WScript.Shell") Set exec = sh.Exec("ini.exe get %APPDATA%\sth\file.ini ""Section name"" key") sFirma1 = exec.StdOut.ReadLine Call sh.Run("ini.exe set %APPDATA%\sth\file.ini ""Section name"" key set_value", 0) 

See also Silent command line operation with VbScript and receiving output? .

This is the executable file code:

 #include <stdio.h> #include <windows.h> void usage() { puts("ini <get>/<set> <file> <section> <key> <value>"); exit(1); } int main(int cArg, char **aszArg) { int iFile = 2; int iSection = 3; int iKey = 4; int iValue = 5; if (cArg < 5) usage(); if (strcmp(aszArg[1], "get") != 0 && strcmp(aszArg[1], "set") != 0) usage(); if (strcmp(aszArg[1], "set") == 0 && cArg < iValue + 1) usage(); if (strcmp(aszArg[1], "set") == 0) { if (!WritePrivateProfileString(aszArg[iSection], aszArg[iKey], aszArg[iValue], aszArg[iFile])) puts("Failure in WriteProfileString."); } else { char buf[1000]; buf[0] = 0; GetPrivateProfileString( aszArg[iSection], aszArg[iKey], "", buf, 999, aszArg[iFile]); puts(buf); } return 0; } 

You need to compile it using the c compiler for Windows. I did this with gcc, but the free compiler from ms should also work. If this page with the 32-bit executable is still available, you can try, but at your own risk. Hackers have already visited my site once.

0
source

All Articles